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 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码