【问题标题】:.htaccess RewriteRule keeping URL structure.htaccess RewriteRule 保持 URL 结构
【发布时间】:2015-10-05 07:08:55
【问题描述】:

我目前的重写规则:

RewriteEngine on
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?a=$1&v=$2&id=$3 [L]

上面的结果很好,所以我可以像这样格式化网址

domain.com/a/b/c

我也想添加一个域切换,所以我想要的结果是

sub.domain.com/a/b/c 当您使用 domain.com/a/b/c 访问它时

目前这是我尝试过的

RewriteEngine on
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?a=$1&v=$2&id=$3 [L]
RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule (.*)$ http://sub.domain.com/ [R=301,L]

但是这样做的结果是

http://sub.domain.com/a=a&v=b&id=c

并且需要

http://sub.domain.com/a/b/c

感谢您的帮助!

【问题讨论】:

    标签: regex linux apache .htaccess mod-rewrite


    【解决方案1】:

    颠倒你的规则顺序:

    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
    RewriteRule (.*)$ http://sub.domain.com/$1 [R=301,L]
    
    RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)?$ index.php?a=$1&v=$2&id=$3 [L,QSA]
    

    请务必在清除浏览器缓存后对此进行测试。

    【讨论】:

      猜你喜欢
      • 2011-05-03
      • 2013-02-03
      • 2014-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多