mod_rewrite为什么500内部服务器错误
问题描述:
我有一堆域我想重定向(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,因为会有很多这样的组合。
是否有任何优雅和简单的方法来解决这个最少的代码?
答
你很近。我会改变它一点,所以没有重复,你保证有一个有效的网址。
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
标志的文档,看看为什么我用的。