apache 重写查询字符串

hrirmatl  于 2022-11-25  发布在  Apache
关注(0)|答案(2)|浏览(105)

我有这个URL:

oldsite.example/profile.php?uid=10

我想将其改写为:

newsite.example/utenti/10

我怎么能那样做呢?
我写的是这样的:

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

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

yeotifhr

yeotifhr1#

要在重写条件中使用匹配项,必须使用%1而不是$1

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

0ejtzxu12#

$n 仅引用RewriteRule指令的匹配项。使用%n 可引用对应的RewriteCond指令的匹配项。
此外,您需要为替换指定一个空查询。否则将使用原始查询。
如果希望查询的其余部分保持不变,请使用以下规则:

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

相关问题