【问题标题】:How to make the settings of custom 403 page and url rewriting coexist?如何让自定义403页面和url重写的设置并存?
【发布时间】:2018-02-04 03:55:25
【问题描述】:

在我的 .htaccess 文件中,我有将 http://www.myownwebsite.com/about.php 等 URL 重写为 http://www.myownwebsite.com/about/ 的设置。

我还有自定义错误 403 和 404 页面的设置。

我遇到的问题是,当我测试直接访问文件夹(例如 http://www.myownwebsite.com/css/)时,我看到的不是自定义 403 页面,而是自定义 404 页面。我猜是因为url重写的设置覆盖了自定义403页面的设置。

这是我的 .htaccess 文件中的相关代码:

ErrorDocument 403 /403.php
ErrorDocument 404 /404.php

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

我做错了什么?

【问题讨论】:

    标签: .htaccess url-rewriting http-status-code-404 http-status-code-403


    【解决方案1】:

    在将 .php 添加到您的 URI 之前,请确保存在相应的 .php 文件。

    最好将重定向规则放在其他规则之前。

    像这样:

    Options -MultiViews -Indexes
    ErrorDocument 403 /403.php
    ErrorDocument 404 /404.php
    
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
    RewriteRule (.*)$ /$1/ [R=301,L]
    
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^([^/]+)/$ $1.php [L]
    
    RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f
    RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php [L]
    

    【讨论】:

    • 我明白了。非常感谢您的建议。我会将重定向规则放在其他规则之前。
    猜你喜欢
    • 1970-01-01
    • 2014-12-21
    • 2015-04-04
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 2012-07-13
    • 2013-06-13
    • 2014-12-19
    相关资源
    最近更新 更多