htaccess query_string仅在字符串等于特定值时重定向

时间:2012-01-24 19:04:24

标签: .htaccess

我一直在寻找如何做到这一点的几个小时,感觉好像我很接近。

我正在尝试根据网址中传递的字符串参数将特定访问者重定向到我网站上的其他网页。

例如,我想将URL mysite.com/index.php?src=msn&test=1重定向到mysite.com/msn/index.php?src=msn&test=1

我计划在我的普通网站访问者访问index.php页面而不重新定向,所以它只应在查询字符串与我设置的值匹配时重定向。

这是我的代码:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^src=msn(.*)$
RewriteRule ^(.*)$ http://www.mysite.com/msn/ [L]

上面的代码在我访问网址mysite.com/?src=msn&test=1时有效,但它不适用于mysite.com/index.php?src=msn&test=1。当我输入index.php时,知道如何让它工作吗?

感谢。

EDIT :::

我的.htaccess文件中也有以下规则:

 ErrorDocument 404 /error404.php

 <IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /blog/
 RewriteRule ^index\.php$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . /blog/index.php [L]
 </IfModule>

 RewriteEngine on
 RewriteCond %{HTTP_HOST} ^mywebsite\.com$ [NC]
 RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [R=301,L]

第一个是404重定向,第二个是wordpress文件更改,最后一个强制所有页面都是http://www

1 个答案:

答案 0 :(得分:1)

这一行

  

RewriteRule ^ index.php $ - [L]

阻止index.php进一步处理。

最简单的解决方案是将新规则移到顶部,如下所示(我假设它们都在同一个.htaccess文件中)

ErrorDocument 404 /error404.php

Options +FollowSymlinks
RewriteEngine on
RewriteBase /blog/

#rule to add www to domain
RewriteCond %{HTTP_HOST} ^mywebsite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [R=301,L]

#new rule to redirect based on query string param
RewriteCond %{QUERY_STRING} ^src=msn(.*)$
#unless it is already msn
RewriteCond %{REQUEST_URI} !^/msn/ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/msn/ [L,R]

#rules for blogs
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
相关问题