2014-07-07 51 views
1

我需要在我的wordpress網站上抓到所有的404,並根據原始url請求重定向它們。例如,如果下面是一個404.htaccess中的自定義Wordpress 404s(複數)

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 

但不是重定向到我的定製-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頁

回答

1

的WordPress有它的處理404內,您的WP主題,你可以創建一個名爲404.php和文件裏面有一個PHP代碼,以自己的方式:

  1. 讀取當前REQ uest URI使用$_SERVER['REQUEST_URI']
  2. 從URI找到/
  3. 使用substring功能最後指數上/後襬脫的一部分,並重定向