2016-11-30 53 views
0

我有一堆域我想重定向(301)到domain.com,保留查詢字符串和其他參數。mod_rewrite爲什麼500內部服務器錯誤

看起來我有點生鏽mod_rewrite因爲我找不到這個問題的簡單解決方案。

RewriteEngine On 
RewriteCond %{QUERY_STRING} ^$ 
RewriteRule ^(.*)$ http://domain.com/?utm_source=%{HTTP_HOST}&utm_medium=redirection [R=301] 
RewriteRule ^(.*)$ http://domain.com/?%{QUERY_STRING}&utm_source=%{HTTP_HOST}&utm_medium=redirection [R=301] 

我想它:

IF查詢字符串是空的: %{HTTP_HOST} & utm_medium =重定向,其中%{HTTP_HOST}應被解析爲源域(保留查詢字符串)

ELSE(IF查詢字符串不爲空): 重寫規則^(*)$ http://domain.com/?% {QUERY_STRING} & utm_source =%{HTTP_HOST} & utm_medium =重定向[R = 301]

我不想爲每個我要重定向的域編寫RewriteCond,因爲會有很多這樣的組合。

是否有任何優雅和簡單的方法來解決這個最少的代碼?

回答

0

你很近。我會改變它一點,所以沒有重複,你保證有一個有效的網址。

RewriteEngine On 
# append an & to the query if it exists 
RewriteCond %{QUERY_STRING} .+ 
RewriteCond %0& .+ 
RewriteRule^- [E=Q:%0] 
# get host so it can be escaped with B flag 
RewriteCond %{HTTP_HOST} .* 
RewriteRule^http://domain.com/?%{ENV:Q}utm_source=%0&utm_medium=redirection [NE,B,L,R=301] 

閱讀RewriteRule標誌的文檔,看看爲什麼我用的。