dtf1111 2014-10-01 12:41
浏览 46
已采纳

htaccess重写url和重定向

I am wondering how to rewrite this url profile.php?user=1 to /user/1. I know that this can be done by .htaccess. Here is what i have tried and it worked fine but the only problem is that it

RewriteCond %{THE_REQUEST} \s/+profile\.php\?user=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ profile.php?user=$1 [L,QSA]

Besides these codes, my .htacces also has these lines

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.*$ [NC] 
RewriteRule \.(gif|jpg|png)$ http://www.domain.com [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
ErrorDocument 404 /error404.php
ErrorDocument 403 /error404.php
Options -Indexes 

RewriteCond %{THE_REQUEST} \s/+page\.php\?pagename=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /page.php?pagename=$1 [L,QSA]

Please also tell me if my .htaccess page is correct. I am not good at htaccess

  • 写回答

2条回答 默认 最新

  • dongzang5815 2014-10-01 20:26
    关注

    Change this:

    RewriteCond %{THE_REQUEST} \s/+profile\.php\?user=([^\s&]+) [NC]
    RewriteRule ^ /%1? [R=302,L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/?$ profile.php?user=$1 [L,QSA]
    

    to:

    RewriteCond %{THE_REQUEST} \s/+profile\.php\?user=([^\s&]+) [NC]
    RewriteRule ^ /user/%1? [R=302,L]
    
    RewriteRule ^user/([^/]+)/?$ profile.php?user=$1 [L,QSA]
    

    And make sure the page has either absolute links or add this to the page's header:

    <base href="/" />
    

    otherwise, all your links will be broken.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?