我在 htaccess
中使用以下内容文件去除 .php
我的文件的扩展名。它工作正常。
但是,我也想利用 ErrorDocument 404 /index.php
将任何不存在的 url 重定向回主页。如果我使用 ErrorDocument 404 /index.php
独自在htaccess
,重定向工作正常。另外,如果我使用下面的重写代码,它会成功删除 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]
最佳答案
不确定您遇到的问题究竟是什么,但是如果我将您的规则更改为此,它对我有用:
ErrorDocument 404 /index.php
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
因此,与其检查请求是否未访问存在的实际文件/目录,不如检查该文件的 .php 版本是否存在。
关于.htaccess - htaccess ErrorDocument 404 与 RewriteEngine On 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9186031/