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

时间:2016-11-30 22:51:14

标签: apache .htaccess redirect mod-rewrite

我有一堆域名我希望被重定向(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]

我希望:

如果查询字符串为空: 301到http://domain.com/?utm_source= %{HTTP_HOST} & utm_medium =重定向,其中%{HTTP_HOST}应解析为源域(保留查询字符串)

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

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

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

1 个答案:

答案 0 :(得分:0)

你非常接近。我会稍微改变一下,所以没有重复,你保证有一个有效的URL。

tv[i].setOnClickListener(new OnClickListener() 
{
    @Override
    public void onClick(View v) 
    {
       // call your desired method with different values here 

    }
});

阅读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] 标记上的文档,了解我使用这些文档的原因。

相关问题