.htaccess;无需延期

发布于 2024-11-17 16:32:14 字数 256 浏览 2 评论 0原文

我需要一些 .htaccess 代码,将无扩展名文件视为 PHP 文件。

假设访问者访问www.mywebsite.com/dir1/dir2/file.name,它首先会查找文件dir1/dir2/file.name是否存在,如果不存在如果存在,它将查找 dir1/dir2/file.name.php (因此带有 .php 扩展名)。

这在某种程度上可能吗?

I need some .htaccess code that will treat extensionless files as PHP files.

Suppose the visitors visits www.mywebsite.com/dir1/dir2/file.name, it will first look the file dir1/dir2/file.name exists and if it not exists, it will look for dir1/dir2/file.name.php (so with .php extension).

Is that possible in some way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

南七夏 2024-11-24 16:32:14

您可以编写一个重写规则,该规则仅在请求的文件不存在时才生效。

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !\.php$
RewriteRule ^(.*)$ $1.php [L]

You can write a rewriting rule which only takes into effect if the requested file doesn't exists.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !\.php$
RewriteRule ^(.*)$ $1.php [L]
与他有关 2024-11-24 16:32:14

你应该看看 Apache 的 选项 指令,最具体的是 http://httpd.apache.org/docs/2.0/content-negotiation.html 选项,您可以将其包含在 .htaccess 文件即可实现此目的。

添加到您的.htaccess

Options +MultiViews

You should have a look at Apache's Options directive, most specifically the http://httpd.apache.org/docs/2.0/content-negotiation.html option which you can include in your .htaccess file and will do just that.

Add to your .htaccess:

Options +MultiViews
浅黛梨妆こ 2024-11-24 16:32:14

好吧,答案是答案的组合。我需要在我的 Apache 配置中添加 Options +MultiViews 。然后我将 Andrews 的回答扩展到以下内容:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.php [L]

问候并非常感谢!
凯文

Ok, the answer was a combination of answers. I needed to add Options +MultiViews in my Apache configuration. Then I extend Andrews answer to the following:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.php [L]

Regards and many thanks!
Kevin

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