且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

基于在.htaccess中GET变量的301重定向的URL

更新时间:2023-10-21 16:03:16

由于在URL查询参数可以是任意的顺序,你需要使用任何一个的 的RewriteCond 指令的每一个参数检查或为每个可能的permutiation。

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指令 。最简单的是用纯文本文件的的对:

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

要定义地图,写在你的服务器或virual主机配置(该指令是不是在每个目录范围内允许)以下指令:

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]