【问题标题】:how to redirect folders browsing to error 404 page如何将浏览的文件夹重定向到错误 404 页面
【发布时间】:2015-10-14 16:17:53
【问题描述】:

我正在使用 wordpress,并且有一些用户不应该看到和浏览的文件夹。对于这个找到的解决方案 - 在 .htaccess 中添加这一行

Options All -Indexes or Options -Indexes

但它重定向到 403 - 服务器的 FORBIDDEN 页面,我需要重定向到我网站的错误 404 页面。我该怎么做?

【问题讨论】:

    标签: php wordpress .htaccess mod-rewrite redirect


    【解决方案1】:

    您可以使用以下代码在这些目录中创建一个 index.php:

    header('HTTP/1.0 404 Not Found');
    die('Page not found');
    

    【讨论】:

    • 我已经创建了 index.php 和您编写的代码,但它会将我重定向到 ERROR 500 - INTERNAL SERVER ERROR page @Raja Amer Khan
    • 代码没问题,我认为你的 .htaccess 文件中有一些错误的配置。注释掉 htaccess 的规则,然后检查。
    • 这是我的 htaccess # BEGIN WordPress RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} ! -d 重写规则。 /index.php [L] # END WordPress Redirect 404 /folder-name/ @Raja Amer Khan
    • 请参阅本指南以获得您的解决方案:codex.wordpress.org/Creating_an_Error_404_Page
    【解决方案2】:

    您可以尝试使用特定的文件夹名称,例如

    重定向 404 /folder1/

    重定向 404 /folder2/

    【讨论】:

    • 我已经这样做了,现在它重定向到服务器的404页面,我需要重定向我网站的404页面@HEMANT SUMAN
    • 你需要创建自定义404页面添加这一行ErrorDocument 404 /custom_page.html
    • 1. .htaccess 2 中的处理程序不能超过 1 404 个。此解决方案与 403 无关
    【解决方案3】:

    在您的根 wordpress 中,您可以这样做:

    # handles 403 (forbidden) status as 404 (not found)
    ErrorDocument 403 /index.php?error=404
    
    # disables directory listing
    Options -Indexes
    

    【讨论】:

    【解决方案4】:

    将此添加到您网站根目录下的 .htaccess 文件中。

    ErrorDocument 403 /file_not_found.html
    

    并且file_not_found.html 应该在根文件夹中。

    【讨论】:

      【解决方案5】:

      你可以用这个:

      DirectoryIndex /404.php
      

      【讨论】:

      • 同样的问题,它重定向到服务器的 404 页面,我需要重定向到我的 404 页面@Starkeen
      • 它确实重定向到你的404页面,你有/404.php文件吗?
      • @aidaghazaryan:当然,您需要将 /404.php 替换为您的 404 页面的路径。并且那个 404 页面还应该确保(通过脚本)它返回的 HTTP 状态代码是404,无论它是如何执行的。否则这将导致HTTP 200 OK 或类似的结果。
      【解决方案6】:

      只需在这些文件夹中添加 index.html 文件即可找到解决方案,而在 .htaccess 文件中则不添加任何内容。谢谢大家

      【讨论】:

        猜你喜欢
        • 2016-01-06
        • 2015-08-01
        • 2023-03-27
        • 1970-01-01
        • 2011-09-25
        • 1970-01-01
        • 2015-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多