繁体   English   中英

mod_rewrite为什么500内部服务器错误

[英]mod_rewrite Why 500 Internal Server Error

我想将一堆域名(301)重定向到domain.com,同时保留查询字符串和其他参数。

似乎我对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查询字符串是空的:301〜 http://domain.com/?utm_source= %{HTTP_HOST}&utm_medium =重定向,其中%{HTTP_HOST}应被解析为源域(保留查询字符串)

ELSE(如果查询字符串不为空):RewriteRule ^(。*)$ http://domain.com/?% {QUERY_STRING}&utm_source =%{HTTP_HOST}&utm_medium = redirect [R = 301]

我不想为要重定向的每个域编写RewriteCond,因为这样的组合将会很多。

有什么优雅而简单的方法可以用最少的代码来解决这个问题?

你很亲密 我会稍作更改,以确保没有重复,并且可以保证您具有有效的URL。

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标志的文档,以了解为什么我使用了那些标志。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM