简体   繁体   English

如何编写.htaccess重定向以查找所有号码?

[英]How can I write a .htaccess redirect to find all numbers?

I'm trying to figure out a regular expression that will find some numbers in a URL and use them in the URL I redirect to. 我正在尝试找出一个正则表达式,该正则表达式将在URL中找到一些数字并在重定向到的URL中使用它们。

Redirect 301 /page.php?id=95485666 http://test.com/profile/info/id/95485666

i was thinking maybe 我在想

Redirect 301 /page.php?id=([0-9]+) http://test.com/profile/info/id/$1

but it doesn't seem to work 但它似乎不起作用

Also, if I do a 301 redirect, how long do i have to keep the code in the .htaccess file? 另外,如果我执行301重定向,我必须将代码保留在.htaccess文件中多长时间? when is Google gonna figure out that the new link is the good one? Google何时会发现新链接是好的链接?

You can't match against the query string in the Redirect directive (nor in a RedirectMatch / RewriteRule either). 您不能与Redirect指令中的查询字符串匹配(也不能在RedirectMatch / RewriteRule匹配)。 You need to use mod_rewrite's %{QUERY_STRING} var: 您需要使用mod_rewrite的%{QUERY_STRING} var:

RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)id=([0-9]+)($|&)
RewriteRule ^/?page\.php$ http://test.com/profile/info/id/%2? [L,R=301]

Well I guess the syntax is incorrect. 好吧,我猜想语法是不正确的。 Try this: 尝试这个:

RewriteRule ^page.php?id=([0-9]+)  http://test.com/profile/info/id/$1  [R=301,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM