301 基于 .htaccess 中的 GET 变量重定向 URL [英] 301 Redirecting URLs based on GET variables in .htaccess

查看:27
本文介绍了301 基于 .htaccess 中的 GET 变量重定向 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些乱七八糟的旧网址,例如...

I have a few messy old URLs like...

http://www.example.com/bunch.of/unneeded/crap?opendocument&part=1

http://www.example.com/bunch.of/unneeded/crap?opendocument&part=2

...我想重定向到更新、更简洁的表单...

...that I want to redirect to the newer, cleaner form...

http://www.example.com/page.php/welcome

http://www.example.com/page.php/prices

我知道我可以通过简单的重定向将一个页面重定向到另一个页面,即

I understand I can redirect one page to another with a simple redirect i.e.

重定向 301/bunch.of/unneeded/crap http://www.example.com/page.php

Redirect 301 /bunch.of/unneeded/crap http://www.example.com/page.php

但是源页面没有改变,只有 GET 变量.我不知道如何根据这些 GET 变量的值进行重定向.有人可以帮忙吗!?我对旧的正则表达式相当方便,所以如果必须的话我可以使用 mod-rewrite 但我不清楚重写 GET vars 的语法,我更愿意避免性能下降并使用更清洁的重定向指令.有办法吗?如果没有,有人能告诉我正确的 mod-rewrite 语法吗?

But the source page doesn't change, only it's GET vars. I can't figure out how to base the redirect on the value of these GET variables. Can anybody help pls!? I'm fairly handy with the old regexes so I can have a pop at using mod-rewrite if I have to but I'm not clear on the syntax for rewriting GET vars and I'd prefer to avoid the performance hit and use the cleaner Redirect directive. Is there a way? and if not can anyone clue me in as to the right mod-rewrite syntax pls?

干杯,

罗杰.

推荐答案

由于 URL 查询中的参数可能具有任意顺序,因此您需要使用任意一种 RewriteCond 指令,用于检查每个参数或每个可能的排列.

As the parameters in the URL query may have an arbitrary order, you need to use a either one RewriteCond directive for every parameter to check or for every possible permutiation.

以下是每个参数的 RewriteCond 指令示例:

Here’s an example with a RewriteCond directive for each parameter:

RewriteCond %{QUERY_STRING} ^([^&]&)*opendocument(&|$)
RewriteCond %{QUERY_STRING} ^([^&]&)*part=1(&|$)
RewriteRule ^bunch.of/unneeded/crap$ /page.php/welcome? [L,R=301]

RewriteCond %{QUERY_STRING} ^([^&]&)*opendocument(&|$)
RewriteCond %{QUERY_STRING} ^([^&]&)*part=2(&|$)
RewriteRule ^bunch.of/unneeded/crap$ /page.php/prices? [L,R=301]

但是正如你所看到的,这可能会变得一团糟.

But as you can see, this may get a mess.

所以更好的方法可能是使用 RewriteMap.最简单的是带有 keyvalue 对的纯文本文件:

So a better approach might be to use a RewriteMap. The easiest would be a plain text file with key and value pairs:

1 welcome
2 prices

要定义您的地图,请在您的服务器或虚拟主机配置中编写以下指令(该指令在每个目录上下文中是不允许的):

To define your map, write the following directive in your server or virual host configuration (this directive is not allowed in per-directory context):

RewriteMap examplemap txt:/path/to/file/map.txt

那么你只需要一个规则:

Then you would just need one rule:

RewriteCond %{QUERY_STRING} ^([^&]&)*opendocument(&|$)
RewriteCond %{QUERY_STRING} ^([^&]&)*part=([0-9]+)(&|$)
RewriteRule ^bunch.of/unneeded/crap$ /page.php/%{examplemap:%2}? [L,R=301]

这篇关于301 基于 .htaccess 中的 GET 变量重定向 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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