使用.htaccess清理URL并提供可能的建议

Эџa*_*мaи 1 php .htaccess url-rewriting

我有以下网址:

http://mywebsite.com/somepage.php?product=343&type=4

我希望用户能够键入:

http://mywebsite.com/somepage?product=343&type=4

角色应该处理子目录中的页面和不同的文件名(不仅仅是Somepage或subdomain).例如:

http://mywebsite.com/subdomain/somepage.php?product=343&type=4

可以通过键入来访问:

http://mywebsite.com/subdomain/somepage?product=343&type=4

我也想知道这样的链接是否更清洁?

Nat*_*han 5

如果.php文件存在,这将采取任何不指向文件或目录的请求并附加".php".

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
Run Code Online (Sandbox Code Playgroud)

这应该通过仅在目标文件存在时重写请求来解决问题.