【问题标题】:Rewrite URL after redirecting 404 error htaccess重定向 404 错误 htaccess 后重写 URL
【发布时间】:2013-11-26 14:03:42
【问题描述】:

所以我知道这可能看起来有点奇怪,但为了保持一致性,我希望我的所有网址都以这种形式出现:http://domain.com/page/ 到目前为止,我已经让常规页面正常工作,但我似乎无法获得错误页面正常工作。

如果用户访问一个不存在的页面或目录,我希望浏览器硬重定向到:http://domain.com/404/ 然而,这个目录实际上并不存在。错误页面的真实位置将在 /pages/errors/404.php 下

此外,虽然我不需要所有各种错误(400、401、403、404、500)的确切答案,但我将应用任何方法将所有这些错误重定向到它们的“正确”URL(例如http://domain.com/400/http://domain.com/500/等)

有什么想法吗?

【问题讨论】:

    标签: .htaccess redirect url-rewriting rewrite custom-error-pages


    【解决方案1】:

    在你的 .htaccess 中试试这个:

    .htaccess

    ErrorDocument 404 http://example.com/404/
    ErrorDocument 500 http://example.com/500/
    # or map them to one error document:
    # ErrorDocument 404 /pages/errors/error_redirect.php
    # ErrorDocument 500 /pages/errors/error_redirect.php
    
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_URI} ^/404/$
    RewriteRule ^(.*)$ /pages/errors/404.php [L]
    
    RewriteCond %{REQUEST_URI} ^/500/$
    RewriteRule ^(.*)$ /pages/errors/500.php [L]
    
    # or map them to one error document:
    #RewriteCond %{REQUEST_URI} ^/404/$ [OR]
    #RewriteCond %{REQUEST_URI} ^/500/$
    #RewriteRule ^(.*)$ /pages/errors/error_redirect.php [L]
    

    ErrorDocument 将所有 404 重定向到特定 URL,将所有 500 重定向到另一个 URL(替换为您的域)。

    重写规则将该 URL 映射到您的实际 404.php 脚本。如果您愿意,可以使 RewriteCond 正则表达式更通用,但我认为您必须明确定义要覆盖的所有 ErrorDocument 代码。

    本地重定向:

    将 .htaccess ErrorDocument 更改为存在的文件(必须存在,否则会出现错误):

    ErrorDocument 404 /pages/errors/404_redirect.php
    

    404_redirect.php

    <?php
       header('Location: /404/');
       exit;
    ?>
    

    根据错误号重定向

    看起来您需要在 .htaccess 中为要重定向的每个错误指定一个 ErrorDocument 行(请参阅:Apache ErrorDocumentApache Custom Error)。上面的 .htaccess 示例中有多个示例。您可以使用以下作为通用重定向脚本来替换上面的 404_redirect.php。

    error_redirect.php

    <?php
       $error_url = $_SERVER["REDIRECT_STATUS"] . '/';
       $error_path = $error_url . '.php';
    
       if ( ! file_exists($error_path)) {
          // this is the default error if a specific error page is not found
          $error_url = '404/';
       }
    
       header('Location: ' . $error_url);
       exit;
    ?>
    

    【讨论】:

    • 正是我要找的...但是无论如何要在 ErrorDocument 声明中使用相对路径?
    • 如果您使用相对路径,您需要指向一个现有文件,然后该文件可以重定向到不存在的 /404/ url。正在编辑我的答案以显示此内容...
    • 我可以只使用一个文件来重定向任何错误吗?通过使用 url 查询什么的?
    • @LeinardoSmtih - 我再次更新以显示更多示例。希望有帮助!
    【解决方案2】:

    将此代码放入您的 .htaccess 文件中

    RewriteEngine On
    ErrorDocument 404 /404.php
    

    其中404.phpfile name 并且位于根目录。你可以把full path放在这里。

    【讨论】:

    【解决方案3】:

    尝试将此规则添加到您的 htaccess 的 top

    RewriteEngine On
    RewriteRule ^404/?$ /pages/errors/404.php [L]
    

    然后根据(或您拥有的任何其他规则):

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^ http://domain.com/404/ [L,R]
    

    【讨论】:

    • 感谢您的回答,但据我所知,这不适用于 401 和 500 等其他错误形式...
    【解决方案4】:

    在您的 .htaccess 文件中,如果您使用的是 apache,您可以尝试使用

    错误页面规则 - 404

    错误文档 404 http://www.domain.com/notFound.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      相关资源
      最近更新 更多