apache 使用 .htaccess 中选定的查询字符串参数重写 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2111182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:39:50  来源:igfitidea点击:

Rewriting URL with selected query string parameters in .htaccess

apache.htaccessurl-rewriting

提问by mshafrir

I recently changed my CMS, and want to rewrite some of my URLs to match the new URL/query string parameter format.

我最近更改了我的 CMS,并且想要重写我的一些 URL 以匹配新的 URL/查询字符串参数格式。

The old URL was:

旧网址是:

http://www.mysite.com/search.cgi?tag=foo&blog_id=bar&other=baz

The new URL should be:

新 URL 应该是:

http://www.mysite.com/?s=foo

In other words, there were several query string parameters in the old format, but I only care to rewrite the tagparam to swhile keeping the same value. The other parameters should be discarded. Of course the order of the parameters shouldn't matter. It also shouldn't matter whether tag is the only parameter or not.

换句话说,旧格式中有几个查询字符串参数,但我只关心在保持相同值的情况下将标签参数重写为s。其他参数应该被丢弃。当然,参数的顺序应该无关紧要。tag 是否是唯一的参数也无关紧要。

Any ideas?

有任何想法吗?

回答by

You can try to use regular expression and grouping, if your server supports that. If I am not mistaken you have to rewrite only one parameter, I guess you could try something like (if you are using apache with mod_rewrite):

如果您的服务器支持,您可以尝试使用正则表达式和分组。如果我没记错的话,您只需要重写一个参数,我想您可以尝试类似的操作(如果您使用带有 mod_rewrite 的 apache):

RewriteCond %{QUERY_STRING} ^.*(\btag\b=(\w+|&\w+;)+)
RewriteRule ^(.+) /?%1 [L]

Edit:I improved the regular expression a little bit to match "tag" regardless of its position in the query string and to preserve special characters sequences such as &In addition it should avoid matches with similar parameters (i.e.: it wouldn't match a parameter called "alttag").

编辑:我稍微改进了正则表达式以匹配“标签”而不管它在查询字符串中的位置并保留特殊字符序列,例如&此外它应该避免与类似参数的匹配(即:它不会匹配参数称为“alttag”)。

Edit #2:An alternative (especially if you have to filter several parameters) is to use an external program to do the rewrite. This page: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritemap(in particular the section "External rewriting program") contains useful information.

编辑 #2:另一种方法(尤其是当您必须过滤多个参数时)是使用外部程序进行重写。此页面:http: //httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritemap(特别是“外部重写程序”部分)包含有用的信息。

回答by Stephen M. Harris

On an Apache webserver, placing the following rule into .htaccess in your document root will rewrite URLs as you described:

在 Apache 网络服务器上,将以下规则放入文档根目录中的 .htaccess 将按照您的描述重写 URL:

RewriteCond %{QUERY_STRING} .*\btag=([^&]*).*
RewriteRule ^search\.cgi /?s=%1 [R=302]

The rule above uses a 302 redirect, so your browser doesn't "memorize" the redirect rule while you're testing and tweaking the rule. Once you're done testing, change the [R] to [R=301]. A 301 is a permanent redirect, and is better for SEO.

上面的规则使用 302 重定向,因此在您测试和调整规则时,您的浏览器不会“记住”重定向规则。完成测试后,将 [R] 更改为 [R=301]。301 是永久重定向,更适合 SEO。

回答by goodeye

Here is another version to get specifically what the OP meant. @smhmic's answer is close to this. I just prefer explicit beginning-or-ampersand to find the parameter, rather than the word-boundary approach. (The accepted answer is overly complicated, and has some issues.)

这是另一个版本,可以具体了解 OP 的含义。@smhmic 的回答接近于此。我只是更喜欢显式的开头或与号来查找参数,而不是字边界方法。(接受的答案过于复杂,并且存在一些问题。)

RewriteCond %{QUERY_STRING} (?:^|&)tag=([^&]*)
RewriteRule ^search\.cgi$ /?s=%1 [NC,R=302]

This is saying:
(?: )don't capture this as a % number
^|&at beginning of querystring or after an ampersand (this acts like the word boundary, but specifically for urls).
tag=the param we're looking for.
( )capture this, into %1.
[^&]*zero or more characters not an ampersand (and stopping at ampersand or running to the end).

这就是说:
(?: )不要
^|&在查询字符串的开头或与符号之后将其捕获为 % 数字(这就像单词边界,但专门用于 url)。
tag=我们正在寻找的参数。
( )将其捕获到 %1 中。
[^&]*零个或多个字符不是&符号(并在&符号处停止或运行到最后)。