使用 htaccess 删除子目录中的文件扩展名
我正在尝试使用 htaccess 删除文件扩展名,因此 www.mysite.com/file.php
变为 www.mysite.com/file
。
我在 .htaccess
文件中使用以下代码:
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
到目前为止,一切顺利。它落在子文件夹中:www.mysite.com/subfolder/file.php 变为 www.mysite.com/file。
我尝试在子文件夹中上传另一个 .htaccess
文件,但效果仍然相同。感觉应该很简单,但我很挣扎......有人可以帮助我吗?谢谢!
编辑抱歉,大家应该说 - 该文件位于子文件夹中,如下所示:
www.mysite.com/folder/subfolder/file.php
.htaccess 文件位于/folder
,URL 更改为以下格式:
www.mysite.com/subfolder/file
抱歉造成误导。
I'm trying to remove file extensions with htaccess, so www.mysite.com/file.php
becomes www.mysite.com/file
.
I'm using the following code in the .htaccess
file:
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
So far, so good. Where it falls down is in subfolders: www.mysite.com/subfolder/file.php
becomes www.mysite.com/file.
I've tried uploading another .htaccess
file in the subfolder, but it still does the same. It feels like it should be really simple, but I'm struggling...can anyone help me out? Thanks!
Edit Sorry folks, should have said - the file is in a subfolder like so:
www.mysite.com/folder/subfolder/file.php
The .htaccess file is in /folder
, the URL changes to this format:
www.mysite.com/subfolder/file
Apologies for misleading.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是隐藏 .php 扩展名所需的规则。这将进入 DOCUMENT_ROOT 目录中的 .htaccess:
This is the rule you'll need to hide .php extension. This goes into your .htaccess in the DOCUMENT_ROOT directory:
您不应该为此使用重写规则。 Apache 有明确的内置选项来执行您想要执行的操作,称为 MultiViews,这就是您应该使用的:
只需将
Options MultiViews
添加到您的 .htaccess,并确保AllowOverride
配置正确。You shouldn't be using rewrite rules for this. Apache has built-in option explicitly for doing what you're trying to do, called MultiViews, and that's what you should be using:
Just add
Options MultiViews
to your .htaccess, and make sure thatAllowOverride
is properly configured.野外猜测
尝试
A guess in the wild
Try