【问题标题】:Custom Wordpress 404s (plural) in .htaccess.htaccess 中的自定义 Wordpress 404(复数)
【发布时间】:2014-08-28 08:19:14
【问题描述】:

我需要在我的 wordpress 站点中捕获所有 404 并根据原始 url 请求重定向它们。例如,如果以下是 404

http://example.com/news/123.html

我需要将其重定向到

http://example.com/news

我会有多个这样的实例,比如下面的 404

http://example.com/hats/greenhat.html

会去

http://example.com/hats

我不知道如何在 Wordpress 中执行此操作。在我的 .htaccess 文件中,这是我的第一行

ErrorDocument 404 http://example.com/my-custom-404.php

但它并没有重定向到 my-custom-404.php,而是转到默认的 wordpress 404 页面。

这是我的 htaccess 供参考

 ErrorDocument 404 http://example.com/index.php


 <IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase / 

 #Rewrite 404s to the news section
 RewriteCond %{REQUEST_URI} ^/404/$
 RewriteRule ^(.*)$ /news/index.php [R=301,L]

 RewriteRule ^index\.php$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d

 RewriteCond %{HTTP_HOST} !^example.com$ [NC]
 RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

 # THE WordPress
 <IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteRule ^index\.php$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . /index.php [L]
 </IfModule>

在我的文件中作为测​​试,我试图将所有 404 重写为 /news/index.php,但这是行不通的 - 一切仍将进入 wordpress 404 页面

【问题讨论】:

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


    【解决方案1】:

    Wordpress 有自己处理 404 的方式。在您的 WP 主题中,您可以创建一个名为 404.php 的文件,并在该文件中包含一个 PHP 代码:

    1. 使用$_SERVER['REQUEST_URI']读取当前请求URI
    2. 从 URI 中找到 / 的最后一个索引
    3. 使用substring 函数删除最后一个/ 之后的部分并重定向

    【讨论】:

      猜你喜欢
      • 2018-03-20
      • 2014-05-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-13
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      相关资源
      最近更新 更多