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

查看:22
本文介绍了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 对这个 URL 进行 301 重定向:

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天全站免登陆