Redirect old page url after .htaccess url rewriting

三世轮回 提交于 2019-12-12 04:03:31

问题


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 (.*) to page/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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!