How to hide .php extension in .htaccess
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
Remove .php extension with .htaccess
我正在尝试隐藏.php文件扩展名,但由于某种原因无法使其正常工作。 我最近的尝试是:
1 2 3 4 | <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^folder/([a-zA-Z_\\-0-9]+)/?$ /folder/$1.php </IfModule> |
我尝试了许多在网上找到的各种代码变体,但仍然没有运气。 .htaccess文件位于根目录中。
我用了这个:
1 2 3 4 5 6 7 8 9 10 11 12 | RewriteEngine On # Unless directory, remove trailing slash RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L] # Redirect external .php requests to extensionless URL RewriteCond %{THE_REQUEST} ^(.+)\\.php([#?][^\\ ]*)?\\ HTTP/ RewriteRule ^(.+)\\.php$ http://example.com/folder/$1 [R=301,L] # Resolve .php file for extensionless PHP URLs RewriteRule ^([^/.]+)$ $1.php [L] |
另请参阅:这个问题
使用PHP脚本无扩展名的另一种选择是
1 | Options +MultiViews |
甚至只是跟随目录
1 | DefaultType application/x-httpd-php |
后者允许将所有不带扩展名
1)您确定启用了mod_rewrite模块吗?检查
2)您的上述规则假定URL以"文件夹"开头。它是否正确?您是否真的想在URL中包含文件夹?这将与以下网址匹配:
1 | /folder/thing -> /folder/thing.php |
如果你真的想要
1 | /thing -> /folder/thing.php |
您需要从匹配表达式中删除文件夹。
我通常使用它来将请求路由到没有php的页面(但是您的应该可以工作,这使我认为可能未启用mod_rewrite):
1 | RewriteRule ^([^/\\.]+)/?$ $1.php [L,QSA] |
3)假设您在.htaccess文件中声明规则,那么您的安装是否允许在.htaccess文件中设置选项(AllowOverride)替代?一些共享主机没有。
When the server finds an .htaccess file (as specified by
AccessFileName) it needs to know which directives declared in that
file can override earlier access information.