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条)

报告相同问题?

悬赏问题

  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里