.htaccess RewriteRule保持URL结构

时间:2015-07-16 01:39:07

标签: regex linux apache .htaccess mod-rewrite

我目前的重写规则:

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

感谢您的帮助!!

1 个答案:

答案 0 :(得分: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]

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