Remove page + query string from URL

2019-07-25 12:42发布

问题:

I have an old URL syntax and since I changed my CMS, the old syntax doesn't work anymore and I have a lot of error 404. I want to delete "article.php?p=" here :

  • http://www.example.com/article.php?p=name_of_my_page

to :

  • http://www.example.com/name_of_my_page

How can I totally remove the string "article.php?p=" from my URLs with htaccess please ? I tried a lot of things but it seems too difficult for me.

回答1:

To remove article.php?p= use the following:

RewriteEngine On
RewriteRule ^([^/]*)\.php$ /article.php?p=$1 [L]

You can use this as an alternative:

RewriteCond %{QUERY_STRING} (^|&)p=name_of_my_page($|&)
RewriteRule ^article\.php$ /name_of_my_page?&%{QUERY_STRING}


回答2:

To redirect the old URL to the new one, you must first capture the page's name from the query string with RewriteCond and then use this in RewriteRule

RewriteCond &%{QUERY_STRING}& &p=(.*?)&
RewriteRule ^article\.php$ /%1? [R,L]