如何在 .htaccess 中隐藏 .php 扩展名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8371634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to hide .php extension in .htaccess
提问by adamhuxtable
Possible Duplicate:
Remove .php extension with .htaccess
可能的重复:
使用 .htaccess 删除 .php 扩展名
I'm trying to hide the .php file extension but for some reason can't get it to work. My latest attempt was the following:
我试图隐藏 .php 文件扩展名,但由于某种原因无法让它工作。我最近的尝试如下:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^folder/([a-zA-Z_\-0-9]+)/?$ /folder/.php
</IfModule>
I have tried many different variances of code I have found online but still no luck. The .htaccess file is placed within the root directory.
我已经尝试了许多不同的代码变体,但我在网上找到了但仍然没有运气。.htaccess 文件位于根目录中。
回答by ryanve
I've used this:
我用过这个:
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/ [R=301,L]
# Redirect external .php requests to extensionless URL
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/ [R=301,L]
# Resolve .php file for extensionless PHP URLs
RewriteRule ^([^/.]+)$ .php [L]
See also: this question
另见:这个问题
回答by mario
The other option for using PHP scripts sans extension is
使用无扩展名的 PHP 脚本的另一个选项是
Options +MultiViews
Or even just following in the directories .htaccess
:
或者甚至只是在目录中跟随.htaccess
:
DefaultType application/x-httpd-php
The latter allows having all filenames without extension script
being treated as PHP scripts. While MultiViews makes the webserver look for alternatives, when just the basename is provided (there's a performance hit with that however).
后者允许将所有没有扩展名的文件名script
视为 PHP 脚本。虽然 MultiViews 使网络服务器寻找替代方案,但当只提供基本名称时(但是这会影响性能)。
回答by typeoneerror
1) Are you sure mod_rewrite module is enabled? Check phpinfo()
1) 您确定启用了 mod_rewrite 模块吗?查看phpinfo()
2) Your above rule assumes the URL starts with "folder". Is this correct? Did you acutally want to have folder in the URL? This would match a URL like:
2)您的上述规则假定 URL 以“文件夹”开头。这样对吗?你真的想在 URL 中有文件夹吗?这将匹配如下 URL:
/folder/thing -> /folder/thing.php
If you actually want
如果你真的想要
/thing -> /folder/thing.php
You need to drop the folder from the match expression.
您需要从匹配表达式中删除文件夹。
I usually use this to route request to page without php (but yours should work which leads me to think that mod_rewrite may not be enabled):
我通常使用它来将请求路由到没有 php 的页面(但你的应该可以工作,这让我认为可能未启用 mod_rewrite):
RewriteRule ^([^/\.]+)/?$ .php [L,QSA]
3) Assuming you are declaring your rules in an .htaccess file, does your installation allow for setting Options (AllowOverride) overrides in .htaccess files? Some shared hosts do not.
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.
当服务器找到 .htaccess 文件(由 AccessFileName 指定)时,它需要知道在该文件中声明的哪些指令可以覆盖先前的访问信息。