问题
I have rewrited my url's with htaccess and now I want to redirect the old url's to the new ones, but I can't figure it out how to do it after all.
This is my redirect rule used:
RewriteRule ^page/([^/]*)/$ /page.php?name=$1 [L]
The old url's look like this: page.php?name=page-name
The new url's look like this: /page/page-name/
回答1:
That's a bit complex when you want redirect an url with GET parameters.
Here's a trick to do it :
RewriteRule ^page\.php$ %{QUERY_STRING} [C]
RewriteRule name=(.*) /page/$1/? [R=301,L]
Explainations :
- First, you redirect
page.php?name=page-name
to?name=page-name
- Then, you ask using the following rule for this result (with
[C]
tag) - Third, you redirect
page-name
, picked with(.*)
topage/page-name/
- Last trick, if you don't put the last
?
, your query string will be appended to your result and you'll have this kind of url :page/page-name/?name=page-name
. Using an useless?
erase the old GET parameters.
Found some informations here :
- Apache mod_rewrite doc
- How to strip a query string
来源:https://stackoverflow.com/questions/11011526/redirect-old-page-url-after-htaccess-url-rewriting