【问题标题】:How to point Wordpress 404 to URL through .htaccess [closed]如何通过 .htaccess 将 Wordpress 404 指向 URL [关闭]
【发布时间】:2018-07-12 15:19:43
【问题描述】:

我在实际页面中创建了一个 404 页面。

ErrorDocument 404 /404.html

上面的代码是用来重定向404的,但这是指向文件而不是URL?

如何将 404 重定向到 CMS 中的页面?

我尝试了以下方法,但无济于事:

ErrorDocument 404 https://www.mywebsite.com/my-404-page

非常感谢

【问题讨论】:

    标签: linux wordpress .htaccess redirect http-status-code-404


    【解决方案1】:

    要使用您建议的方法,您需要包含重写规则:

    RewriteRule ^404/?$ my-404-page.html
    

    那么它会是:

    RewriteRule ^404/?$ my-404-page.html
    ErrorDocument 404 https://www.mywebsite.com/my-404-page/
    

    确保在测试之前清除缓存。

    【讨论】:

      【解决方案2】:

      试试这个答案:

      https://wordpress.stackexchange.com/questions/267781/how-to-redirect-only-404-pages-with-htaccess-in-wordpress

      据我了解,.htaccess 重写规则是在 WordPress 甚至 PHP 引擎处理请求之前执行的,因此您无法知道 URL 是否会在 .htaccess 中触发 404 错误,您无法检查是否给定的路径将在该级别触发 404。您需要在 WordPress 本身中执行此操作:

      // See https://developer.wordpress.org/reference/hooks/template_redirect/
      add_action( 'template_redirect', 'cyb_redirect_not_found_paths' );
      function cyb_redirect_not_found_paths() {
      
          // See https://developer.wordpress.org/reference/functions/is_404/
          if( is_404() ) {
      
              if( $_SERVER['REQUEST_URI'] == '/your_path' ) {
      
                  $url_to = 'redirecton_rul';
      
                  // See https://developer.wordpress.org/reference/functions/wp_redirect/
                  wp_redirect( $url_to );
                  exit;
      
              }
      
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-01
        • 2020-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-13
        相关资源
        最近更新 更多