使用 htaccess 删除子目录中的文件扩展名

发布于 2025-01-02 22:41:09 字数 762 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

臻嫒无言 2025-01-09 22:41:10

这是隐藏 .php 扩展名所需的规则。这将进入 DOCUMENT_ROOT 目录中的 .htaccess:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On

# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

This is the rule you'll need to hide .php extension. This goes into your .htaccess in the DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On

# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
旧街凉风 2025-01-09 22:41:10

您不应该为此使用重写规则。 Apache 有明确的内置选项来执行您想要执行的操作,称为 MultiViews,这就是您应该使用的:

MultiViews的效果如下:如果服务器收到对/some/dir/foo的请求,如果/some/dir启用了MultiViews,并且/some/dir/foo不存在,那么服务器读取目录查找名为 foo.* 的文件,并有效地伪造一个命名所有这些文件的类型映射,为它们分配相同的媒体类型和内容编码,如果客户端按名称请求其中一个文件,则它们将具有相同的媒体类型和内容编码。然后,它会选择最符合客户要求的方案。

只需将 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:

The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements.

Just add Options MultiViews to your .htaccess, and make sure that AllowOverride is properly configured.

终遇你 2025-01-09 22:41:10

野外猜测
尝试

  RewriteRule ^(.+)$ $1.php [NC,L]

A guess in the wild
Try

  RewriteRule ^(.+)$ $1.php [NC,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文