Rewrite all queries to not need the .php extension using a mod_rewrite RewriteRule

本小妞迷上赌 提交于 2019-11-26 18:25:04

问题


I'd like all queries like

http://mysite.com/something/otherthing?foo=bar&x=y

to be rewritten as

http://mysite.com/something/otherthing.php?foo=bar&x=y

In other words, just make the .php extension optional, universally.


回答1:


I would do it this way. Basically, if file doesn't exist, try adding .php to it.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ $1.php [QSA,L]



回答2:


If you can change the httpd.conf and what to, you can also put:

ForceType application/x-httpd-php

in the file as it will force all the paths called to be PHP files. I think this also works with query strings.




回答3:


This works:

RewriteCond %{QUERY_STRING} ^.+$
RewriteRule ^/?([^/\.]+)$ /$1.php [L]

The idea is to make sure there's a query string (question mark plus stuff) and if so check if the stuff before the question mark has no extension and if so, append .php.




回答4:


Something like...

RewriteRule /something/(.+)?(.+) /something/$1.php?$2

would probably work.




回答5:


Matches only paths with no extension:

RewriteRule ^(([^/]+/+)*[^\.]+)$ $1.php

Edit: In my testing, the query string gets passed on automatically. If it doesn't, you can use this instead:

RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(([^/]+/+)*[^\.]+)$ $1.php?%1


来源:https://stackoverflow.com/questions/183921/rewrite-all-queries-to-not-need-the-php-extension-using-a-mod-rewrite-rewriteru

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