首页 文章

.htaccess重写多个网址

提问于
浏览
0

我需要帮助重写.htaccess文件中的重写 . 所以这就是我现在所拥有的这个有效但当我尝试添加一个新的RewriteRule时没有任何反应 . 我想要重写的网址是index.php?page = $ 1

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1

所以当我这样做时:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*)$ index.php?page=$1

当我这样做时页面没有任何CSS:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*_)$ index.php?page=$1

页面有css,但我仍然得到index.php?page = pagetitle . 但是 Profiles 页面确实给了我/用户名 .

2 回答

  • 0
    RewriteRule ^(.*)$ profile.php?username=$1
    RewriteRule ^(.*)$ index.php?page=$1
    

    您要求服务器将每个URL重定向到两个不同的页面,它无法正常工作,服务器无法猜测要加载哪个页面 .

    您需要的是/ profile / username规则或/ page / pagetitle规则 . IE之类的东西:

    RewriteRule ^profile/(.*)$ profile.php?username=$1 [QSA]
    RewriteRule ^(.*)$ index.php?page=$1 [L]
    
  • 1

    您的重写规则基于正则表达式,因此需要尽可能具体,以便服务器可以确切地确定要使用的URL - 例如,如何判断http://example.com/something是页面还是配置文件?在您的网址上使用"user","profile"等前缀意味着http://example.com/profile/something可以重定向为用户名,其他所有内容都具有默认重定向 . 要实现此目的,您需要首先使用更具体的模式匹配(用户),并使用 [L] 指令指示不应处理以下规则 . 我通常使用URL的负字符类来匹配除正斜杠之外的任何内容 - [^/]* .

    # Enable mod_rewrite
    RewriteEngine On
    # Set the base directory
    RewriteBase /
    # Don't process if this is an actual file or directory
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # Does this url start with /profile and then followed with additional characters?
    RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,L]
    # Assume everything else is a page
    RewriteRule ^(.*)$ index.php?page=$1 [NC,L]
    

    http://htaccess.madewithlove.be/进行测试(请注意,测试不支持 %{REQUEST_FILENAME}%{REQUEST_FILENAME} ) .

    Profile

    input url
    http://www.example.com/profile/something
    
    output url
    http://www.example.com/profile.php
    
    debugging info
    1 RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,QSA,L]  
        This rule was met, the new url is http://www.example.com/profile.php
        The tests are stopped because the L in your RewriteRule options
    2 RewriteRule ^(.*)$ index.php?page=$1 [NC,L]
    

    Page

    input url
    http://www.example.com/something
    
    output url
    http://www.example.com/index.php
    
    debugging info
    1 RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,L]  
    2 RewriteRule ^(.*)$ index.php?page=$1 [NC,L]
        This rule was met, the new url is http://www.example.com/index.php
        The tests are stopped because the L in your RewriteRule options
    

相关问题