htaccess ErrorDocument 404 与 RewriteEngine On 冲突
我在我的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定您遇到的问题到底是什么,但如果我将您的规则更改为此,它对我有用:
因此,不要检查请求是否未访问存在的实际文件/目录,而是检查 .php该文件的版本确实存在。
Not sure what the problem you're having is exactly, but if I change your rules to this, it works for me:
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.