mod_rewrite使用查询字符串重定向URL [英] mod_rewrite to redirect URL with query string

查看:86
本文介绍了mod_rewrite使用查询字符串重定向URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在整个stackoverflow上进行了搜索,但是没有一种答案似乎适用于这种情况.我的httpd.conf文件中已经有很多工作的mod_rewrite规则.我最近才发现Google在其中一个带有查询字符串的未重写URL上建立了索引:

I've searched all over stackoverflow, but none of the answers seem to be working for this situation. I have a lot of working mod_rewrite rules already in my httpd.conf file. I just recently found that Google had indexed one of my non-rewritten URLs with a query string in it:

http://domain.com/?state=arizona

我想使用mod_rewrite 301重定向到此URL:

I would like to use mod_rewrite to do a 301 redirect to this URL:

http://domain.com/arizona

问题是,稍后在我的重写规则中,正在重写第二个URL,以将查询变量传递给WordPress.最终被重写为:

The issue is that later on in my rewrite rules, that 2nd URL is being rewritten to pass query variables on to WordPress. It ends up getting rewritten to:

http://domain.com/index.php?state=arizona

哪个是正确的功能.到目前为止,我尝试过的一切都没有奏效,或者使我陷入了无尽的重写循环.这就是我现在所拥有的,陷入了循环:

Which is the proper functionality. Everything I have tried so far has either not worked at all or put me in an endless rewrite loop. This is what I have right now, which is getting stuck in a loop:

RewriteCond %{QUERY_STRING} state=arizona [NC]
RewriteRule .*   http://domain.com/arizona [R=301,L]
#older rewrite rule that passes query string based on URL:
RewriteRule ^([A-Za-z-]+)$ index.php?state=$1 [L]

这给了我一个无限的重写循环,并带我到这个URL: http://domain.com/arizona?state=arizona

which gives me an endless rewrite loop and takes me to this URL: http://domain.com/arizona?state=arizona

然后我尝试了这个:

RewriteRule .*   http://domain.com/arizona? [R=301,L]

删除了URL中的查询字符串,但仍然创建了一个循环.

which got rid of the query string in the URL, but still creates a loop.

推荐答案

好,添加第二个RewriteCond终于解决了它-现在可以正确重写并且没有循环:

Ok, adding the 2nd RewriteCond finally fixed it - now rewriting correctly and no loop:

# redirect dynamic URL: ?state=arizona
RewriteCond %{QUERY_STRING} state=arizona [NC]
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule .*   http://domain.com/arizona? [R=301,L]
# older rewrite rule that passes query string based on URL:
RewriteRule ^([A-Za-z-]+)$ index.php?state=$1 [L]

下面是使它适用于任何状态值(不仅限于亚利桑那州)的代码:

And here's the code to make it work for any state value, not just arizona:

RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{QUERY_STRING} ^state=([A-Za-z-]+)$ [NC]
RewriteRule .*   http://domain.com/%1? [R=301,L]

这篇关于mod_rewrite使用查询字符串重定向URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆