2012-03-13 170 views
0

我想的.htaccess重写规则

http://mysite.com/?p=47&preview=true 

重写才能

http://mysite.hostingcompany.com/?p=47&preview=true 

但所有其他请求http://mysite.com/行动通常

我的.htaccess看起来是这样的:

RewriteCond ^mysite.com\/?p=(.*)\&preview=true$ 
RewriteRule ^mysite.hostingcompany.com/?p=$1&preview=true$ [L] 

但它不匹配。我必须屠杀我的正则表达,但我不知道如何。

回答

1

RewriteCond匹配变量,如%{HTTP_HOST}%{QUERY_STRING}。在你的情况下,你可以利用它们两个:

# If the requested host is mysite.com 
RewriteCond %{HTTP_HOST} ^mysite\.com$ 
# And the query string matches 
RewriteCond %{QUERY_STRING} p=(\d+) 
RewriteCond %{QUERY_STRING} preview=true 
# Redirect it, appending the querystring as is 
RewriteRule (.*) http://mysite.hostingcompany.com/$1 [L,R,QSA] 
+0

QSA是不需要的,因为查询字符串没有被触摸。 – 2012-03-13 19:30:00

+0

完美。我一直忘记,条件是顺序的,我不必一次做。太感谢了。 – 2012-03-13 19:51:38

+0

答案旁边有一个复选框。如果你点击它,它会标记这个答案是正确的。 – 2012-03-14 19:45:53