简体   繁体   中英

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.

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? when is Google gonna figure out that the new link is the good one?

You can't match against the query string in the Redirect directive (nor in a RedirectMatch / RewriteRule either). You need to use mod_rewrite's %{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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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