htaccess ErrorDocument 404 与 RewriteEngine On 冲突

发布于 2025-01-03 01:19:49 字数 749 浏览 2 评论 0原文

我在我的 htaccess 文件中使用以下内容来删除文件上的 .php 扩展名。效果很好。

但是,我还想利用 ErrorDocument 404 /index.php 将任何不存在的网址重定向回主页。如果我在 htaccess 中单独使用 ErrorDocument 404 /index.php,重定向工作正常。另外,如果我使用下面的重写代码,它会成功删除 php 扩展。然而,我希望这两者能够一起工作。

例如,如果用户输入“www.domain.com/register”,则会转到 www.domain.com/register.php。但是,如果他们输入“www.domain.com/something”,并且 something.php 不存在,那么他们会重定向回 index.php.我已经在我的 htaccess 中尝试了这两者的所有排列,但均无济于事。任何帮助将不胜感激。

Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]

I'm using the below in my htaccess file to strip out the .php extensions on my files. It works fine.

However, I also want to utilize ErrorDocument 404 /index.php to redirect any non existing urls back to the homepage. If I use ErrorDocument 404 /index.php alone in the htaccess, the redirects work fine. Also, if I use the below Rewrite code it successfully strips the php extensions. I would like both of these to work together, however.

For example, if a user enters "www.domain.com/register", they go to www.domain.com/register.php. But if they enter "www.domain.com/something", and something.php doesn't exist, then they redirect back to index.php. I've tried every permutation of both of these together in my htaccess to no avail. Any help would be appreciated.

Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]

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

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

发布评论

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

评论(1

东风软 2025-01-10 01:19:49

不确定您遇到的问题到底是什么,但如果我将您的规则更改为此,它对我有用:

ErrorDocument 404 /index.php

Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]

因此,不要检查请求是否未访问存在的实际文件/目录,而是检查 .php该文件的版本确实存在。

Not sure what the problem you're having is exactly, but if I change your rules to this, it works for me:

ErrorDocument 404 /index.php

Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]

So instead of checking if the request isn't accessing an actual file/directory that exists, check that the .php version of that file DOES exist.

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