Remove page + query string from URL

…衆ロ難τιáo~ 提交于 2019-12-24 07:28:40

问题


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]


来源:https://stackoverflow.com/questions/37697919/remove-page-query-string-from-url

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