.htaccess - htaccess ErrorDocument 404 与 RewriteEngine On 冲突

标签 .htaccess mod-rewrite

我在 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/

相关文章:

php - .htaccess 在 Ubuntu Server x64 上不起作用

php - Apache mod_rewrite : RewriteMap directive using PHP script on Windows machine

linux - 禁止访问文件夹

php - 文件夹访问限制 Apache

.htaccess - htaccess 将子目录名称重写为查询字符串参数

apache - 为浏览器缓存启用 mod_expires

Laravel 从本地主机上的 url 中删除公共(public)

apache - 文件中断的 mod_rewrite 和 htaccess 路径

.htaccess - 如何使用 htaccess 重写在子域上保留 SSL?

php - 我在哪里可以找到/如何创建一个新的 HTTPD Conf