apache 重写查询字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1244881/
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:02:39  来源:igfitidea点击:

Rewrite Query String

apachemod-rewritequery-string

提问by collimarco

I have this URL:

我有这个网址:

oldsite.com/profile.php?uid=10

I would like to rewrite it to:

我想将其改写为:

newsite.com/utenti/10

How can I do that?

我怎样才能做到这一点?

UPDATE: I wrote this:

更新:我写了这个:

RewriteCond %{QUERY_STRING} ^uid=([0-9]+)$
RewriteRule ^profile\.php$ http://www.newsite.com/utenti/ [R=301,L]

But $1 match the full query string and not just the user id.

但是 $1 匹配完整的查询字符串,而不仅仅是用户 ID。

回答by Vinko Vrsalovic

To use matches in the rewrite conditions, you have to use %1 instead of $1. Also, if you wish to remove the rest of the query string you have to append a ?

要在重写条件中使用匹配项,您必须使用 %1 而不是 $1。此外,如果您想删除查询字符串的其余部分,您必须附加一个 ?

RewriteCond %{QUERY_STRING} ^uid=([0-9]+)$
RewriteRule ^profile\.php$ http://www.newsite.com/utenti/%1? [R=301,L]

回答by Gumbo

The $nonly refer to the matches of the RewriteRuledirective. Use %nto reference the matches of the corresponding RewriteConddirective.

$n仅指的比赛RewriteRule指令。使用%n引用相应的匹配RewriteCond指令。

Additionally you need to specify an empty query for the substitution. Otherwise the original query will be used.

此外,您需要为替换指定一个空查询。否则将使用原始查询。

And if you want to have the rest of the query to stay intact, use this rule:

如果您想让查询的其余部分保持完整,请使用以下规则:

RewriteCond %{QUERY_STRING} ^(([^&]*&)*)uid=([0-9]+)(.*)
RewriteRule ^profile\.php$ http://new.example.com/utenti/%3?%1%4 [R=301,L]