2012-03-13 173 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