【问题标题】:Problem in htaccess 404 not found?未找到 htaccess 404 中的问题?
【发布时间】:2023-03-25 07:40:01
【问题描述】:

如果找不到该页面,我想重定向到错误页面。所以我在 .htaccess 文件中添加了这段代码。

# '404 Not Found' error
ErrorDocument 404 /index.php?p=error&code=404

但是当我测试它不会重定向到 index.php 文件。

我的整个 .htaccess 文件代码在这里

#AuthName "Restricted Area" 
#AuthType Basic 
#AuthGroupFile /dev/null 
#require valid-user
#<Files "/site/captcha/">
#  Allow from all
#</Files> 

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on 

## File paths are relative to the Document Root (/)
# '400 Bad Request' error
ErrorDocument 400 /index.php?p=error&code=400
# '404 Not Found' error
ErrorDocument 404 /index.php?p=error&code=404
# '403 Forbidden' error
ErrorDocument 403 /index.php?p=error&code=403
# '401 Unauthorized' error
ErrorDocument 401 /index.php?p=error&code=401
# '500 Internal Server Error' 
ErrorDocument 500 /index.php?p=error&code=500

############################# Pages ##########################################################

RewriteRule ^home/$ index.php [S=1]
RewriteRule ^home$ index.php [S=1]

</IfModule>

【问题讨论】:

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


    【解决方案1】:

    首先——将所有ErrorDocument指令移出&lt;IfModule mod_rewrite.c&gt;——根本不需要这样做。

    现在它告诉 Apache:“仅当 mod_rewrite 启用时才使用 ErrorDocument”。

    如果它现在不适合你,那么很可能是 mod_rewrite 实际上没有启用。但是,如果您将它们放在 &lt;IfModule mod_rewrite.c&gt; 块之外(就在它之前),那么它应该可以工作(只要 .htaccess 被 Apache 识别和处理):

    #AuthName "Restricted Area" 
    #AuthType Basic 
    #AuthGroupFile /dev/null 
    #require valid-user
    #<Files "/site/captcha/">
    #  Allow from all
    #</Files> 
    
    Options +FollowSymLinks
    
    # '400 Bad Request' error
    ErrorDocument 400 /index.php?p=error&code=400
    # '404 Not Found' error
    ErrorDocument 404 /index.php?p=error&code=404
    # '403 Forbidden' error
    ErrorDocument 403 /index.php?p=error&code=403
    # '401 Unauthorized' error
    ErrorDocument 401 /index.php?p=error&code=401
    # '500 Internal Server Error' 
    ErrorDocument 500 /index.php?p=error&code=500
    
    <IfModule mod_rewrite.c>
      RewriteEngine on 
      # Pages
      RewriteRule ^home/?$ index.php [L]
    </IfModule>
    

    附言 我还修改了你的重写规则:将它们合并成一条规则。

    【讨论】:

    • 那么你看到了什么错误?它是共享主机..还是您可以完全控制服务器?出于安全/性能目的,可能根本不处理/忽略 .htaccess 文件。如果它是您的服务器,那么您可能需要在服务器配置中的适当位置添加 AllowOverride All 指令。否则(如果是共享托管服务器)请联系您的托管公司。
    猜你喜欢
    • 1970-01-01
    • 2015-09-06
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    相关资源
    最近更新 更多