htaccess 重定向 url 带参数 [英] htaccess redirect url with parameters

查看:37
本文介绍了htaccess 重定向 url 带参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数百个这样的链接:

I have hundreds of links like this:

http://www.domain.com/index.php?tag=value

我想将所有链接重定向到

I want to redirect all links to

http://www.domain.com/value/

示例:

Link1 http://www.domain.com/index.php?tag=LW1fdX49tR重定向到:http://www.domain.com/LW1fdX49tR/

Link1 http://www.domain.com/index.php?tag=LW1fdX49tR redirect to: http://www.domain.com/LW1fdX49tR/

Link2 http://www.domain.com/index.php?tag=A3kh0QLIrc重定向到:http://www.domain.com/A3kh0QLIrc/

Link2 http://www.domain.com/index.php?tag=A3kh0QLIrc redirect to: http://www.domain.com/A3kh0QLIrc/

Link3 http://www.domain.com/index.php?tag=vXwNR4U9qY重定向到:http://www.domain.com/vXwNR4U9qY/

Link3 http://www.domain.com/index.php?tag=vXwNR4U9qY redirect to: http://www.domain.com/vXwNR4U9qY/

我该怎么做?谢谢!

推荐答案

除了重定向请求之外,您可能还想确保新 url 也确实有效.为此,您将需要外部重定向和内部重写.在下面的示例中,我使用 THE_REQUEST 技巧仅在它是实际请求 url 时触发规则,如果它在内部重写,则不会触发.需要防止死循环.

Besides redirecting the request, you probably want to make sure the new url actually works too. You'll need both an external redirect and an internal rewrite for that. In the example below I use the THE_REQUEST trick to only trigger the rule if it is the actual request url, not if it is rewritten internally. It is required to prevent an infinite loop.

#External redirect with THE_REQUEST trick; change R to R=301 when everything works correctly
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index\.php\?tag=(.*)\ HTTP
RewriteRule ^ /%2? [R,L]

#Internal rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?tag=$1 [L]

这篇关于htaccess 重定向 url 带参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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