.htaccess url rewrite - remove .php remove www. convert ?id=10 to /10

烈酒焚心 提交于 2020-01-17 07:38:19

问题


As the title says, I need a quite complex url rewrite mechanism for a web-app as .htaccess rule. I've searched quite a lot now and tried hundred of different rewrite rules.

So, basically this is what I need:
User goes to: http://www.site.com/product.php?id=12
Server should redirect to: http://site.com/product/12

Once thing to mention:
not all pages do append id's.
So I also have: http://www.site.com/some/page.php
which then should redirect to: http://site.com/some/page

or from http://site.com/anotherone.php to http://site.com/anotherone

You help is much appreciated and thank you a lot in advance for you help!


回答1:


RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)\.php$ http://%1/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)\.php\?id=([0-9]+)$ http://%1/$1/$2 [R=301,L]



回答2:


I supose that you, previously have this mod_rewrite rule active:

Users goes to http://site.com/product/12 and in the browser is showed this URL, and internaly, and only internaly, server serve http://www.site.com/product.php?id=12

Put the first RewriteCond and Rule this:

   RewriteCond %{ENV:REDIRECT_STATUS} !200
   RewriteRule ^(\w+)\.php\?id=(\d*)$ /$1/$2 [R=301]

And add another to remove the .php when ends with .php

   RewriteCond %{ENV:REDIRECT_STATUS} !200
   RewriteRule ^(\w+)\.php$ /$1 [R=301]


来源:https://stackoverflow.com/questions/6964031/htaccess-url-rewrite-remove-php-remove-www-convert-id-10-to-10

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