【问题标题】:apache/.htaccess: incorrect page does not redirect to custom 404apache/.htaccess:不正确的页面不会重定向到自定义 404
【发布时间】:2019-01-11 12:41:54
【问题描述】:

我有一个奇怪的错误,我在 .htaccess 中的 ErrorDocument 404 在所有情况下都不能正常工作。

如果我访问一个不存在的页面,我只会收到消息File not found.

如果我添加了 .html 扩展名,它会显示我创建的自定义 404 页面。

https://website.com/not-found

此链接无效,我只是收到消息File not found.

https://website.com/not-found.html

但是,访问此链接后,我看到了我的自定义 404 页面。

这是我的 .htaccess 文件

<IfModule mod_rewrite.c>

    Options +FollowSymLinks -MultiViews
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+?)/?$ $1.php [NC,L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(.+)\.php$ /$1 [R=301,L]

    ErrorDocument 404 /app/templates/errors/404.php
    ErrorDocument 403 /app/templates/errors/403.php

    # Block access to hidden files and directories.
    RewriteCond %{SCRIPT_FILENAME} -d [OR]
    RewriteCond %{SCRIPT_FILENAME} -f
    RewriteRule "(^|/)\." - [F]

</IfModule>

基本上我剥离了扩展,仅此而已。在其他网站上,此代码运行良好,可能是托管公司的问题吗? 请注意,我无权访问 apache 配置文件

谢谢

【问题讨论】:

标签: php apache .htaccess http-status-code-404


【解决方案1】:

像这样更改您的第一个以检查 .php 文件是否存在:

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

如果没有RewriteCond %{REQUEST_FILENAME}.php -f,它会重写每个非文件和非目录。

【讨论】:

  • 感谢您的帮助。现在我的 htaccess 仅包含您的代码,并且 ErrorDocument 适用于 .html 和 extensionLESS url,但是如果我现在使用 .PHP 访问不存在的页面,则找不到该文件。出现错误,所以基本上和之前相反。
  • 不,它在网站的根目录中!
  • 也可以尝试用RewriteCond %{DOCUMENT_ROOT}/$1.php -f替换RewriteCond %{REQUEST_FILENAME}.php -f
猜你喜欢
  • 2014-12-23
  • 2016-11-21
  • 1970-01-01
  • 2014-09-29
  • 1970-01-01
  • 1970-01-01
  • 2012-10-01
  • 2018-04-18
  • 2019-09-14
相关资源
最近更新 更多