您在這裡

Drupal中對404錯誤進行判斷、重定向

James Qi 在 2013年5月6日 - 09:49 發表

  Drupal網站在Site information的設置中有一個404錯誤頁面可以定制到自己希望的網址,如果你在日志中發現大量404錯誤,而其中很多都是同一個類型可以引導到對用戶更有幫助的内容頁面,那麼就可以在這個定制的404錯誤網址中進行判斷、導向。

  我做的例子是http://ak.postcodebase.com/not_found ,因為來自外部網站的鍊接中,有一部分是這個站内找不到或者錯誤的郵編,就可以根據URI進行判斷:

  • 如果訪問9位郵編頁面不存在但前面5位郵編分類頁面存在,則轉到5位郵編分類頁面;
  • 如果訪問9位郵編頁面不存在且前面5位郵編分類頁面也不存在,則轉到首頁;
  • 如果訪問5位郵編分類頁面不存在,則轉到首頁;
  • 其它不屬于上面的情況,則直接返回404 not found信息。

  程序是用PHP編寫的,跳轉可以用這樣的語句實現:header("Location: $new_url",TRUE,302);

  我們前一陣子在Google Webmaster Tools中發現很多報錯的頁面,當時用修改Apache .htaccess來判斷、重定向的,其實也可以用上面的PHP程序來判斷、定向。直接用.htaccess适用于比較簡單的匹配判斷,而PHP适用于比較複雜的匹配判斷。

 

自由标簽:

回應

下面是更多的幾個例子:

# m.開頭的手機版網站,将URI網址開頭出現的/m/替換為/
RewriteCond %{HTTP_HOST}  ^(m\.|)114\.mingluji\.com$
RewriteCond %{REQUEST_URI} ^\/m\/(.*)$
RewriteRule ^(.*)$ /$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^jpn\.youbianku\.com(.*)$
RewriteCond %{REQUEST_URI} ^\/m\/category\/admin2\/.*$
RewriteRule ^(.*)\%26\%23039;(.*)$ /m/$1\%27$2 [L,R=301,NE]

注意,Apache ReWrite雖然很強大,但條件、匹配、替換等還是有些複雜,可以先限定在少數頁面進行測試,沒有問題了再推廣,免得影響正在使用的網站訪問。

James Qi / 祁勁松

  Drupal 6中葡萄牙語的代碼是pt-pt,Drupal 7中是pt,如果Drupal 6網站中沒有對葡萄牙語的代碼做處理就直接鍊接到Drupal 7的網站中,就會在Drupal 7中出現找不到頁面的報錯,這時候需要修改Drupal 6網站的PHP模闆,還可以修改Drupal 7網站的.htaccess,把錯誤頁面自動重定向到正确的網址,例如:

RewriteCond %{HTTP_HOST} ^(|m\.)(report|profile)\.bizdirlib\.com(.*)$
RewriteCond %{REQUEST_URI} ^\/pt-pt(.*)$
RewriteRule ^pt-pt(.*)$ /pt$1 [L,R=301]

  反過來Drupal 7中的pt指向Drupal 6的pt-pt也需要變換,Drupal 7中的fil語言在Drupal 6中沒有,就可以指向默認的英文版。

James Qi / 祁勁松

RewriteCond %{HTTP_HOST} ^(gb|gbr|nzl|sgp)\.bizdirlib\.com(.*)$
RewriteCond %{REQUEST_URI} ^\/m\/pt-pt(.*)$
RewriteRule ^pt-pt(.*)$ m/pt$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^(gb|gbr|nzl|sgp)\.bizdirlib\.com(.*)$
RewriteCond %{REQUEST_URI} ^\/pt-pt(.*)$
RewriteRule ^pt-pt(.*)$ /pt$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^bizdirlib\.com(.*)$
RewriteCond %{REQUEST_URI} ^\/m\/pt-pt(.*)$
RewriteRule ^pt-pt(.*)$ m/pt$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^bizdirlib\.com(.*)$
RewriteCond %{REQUEST_URI} ^\/pt-pt(.*)$
RewriteRule ^pt-pt(.*)$ /pt$1 [L,R=301]

Google Webmaster Tools中有網址找不到的404報錯:

http://in.mingluji.com/m/mt/m/business_directory/American_Trucking_Tax_A...

鍊接到您網站網頁的域為:

http://in.mingluji.com/m/m/business_directory/American_Trucking_Tax_Assoc

這種網址中帶有/m/m/的頁面不知道是怎麼會出現的,頁面可以訪問,但源代碼中的多語言鍊接就出現了上面那種404錯誤。

需要将/m/m/改為一個/m/,在.htaccess用下面的語句可以實現:

RewriteCond %{HTTP_HOST} ^(.*)\.mingluji\.com$
RewriteCond %{REQUEST_URI} ^\/m\/m\/
RewriteRule ^(.*) /m/$1 [L,R=301]

James Qi / 祁勁松

#下面的寫法有誤,會出現無限循環
#RewriteCond %{HTTP_HOST} ^(|m\.)sale\.mingluji\.com(.*)$
#RewriteCond %{REQUEST_URI} ^\/pt(.*)$
#RewriteRule ^pt(.*)$ /pt-pt$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^(|m\.)sale\.mingluji\.com(.*)$
RewriteCond %{REQUEST_URI} ^\/pt\/(.*)$
RewriteRule ^pt(.*)$ /pt-pt$1 [L,R=301]

#for sale.mingluji.com/pt(pt後面無/)

RewriteCond %{HTTP_HOST} ^(|m\.)sale\.mingluji\.com(.*)$
RewriteCond %{REQUEST_URI} ^\/pt$
RewriteRule ^pt$ /pt-pt [L,R=301]

RewriteCond %{HTTP_HOST} ^(|m\.)sale\.mingluji\.com(.*)$
RewriteCond %{REQUEST_URI} ^\/fil(.*)$
RewriteRule ^fil(.*)$ $1 [L,R=301]

RewriteCond %{HTTP_HOST} ^(chn|are|bel|fra|ita|esp|twn|che|aus|aut|can|hkg|us|government-contractor)\.bizdirlib\.com(.*)$
RewriteCond %{REQUEST_URI} ^\/pt\/(.*)$
RewriteRule ^pt(.*)$ /pt-pt$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^(chn|are|bel|fra|ita|esp|twn|che|aus|aut|can|hkg|us|government-contractor)\.bizdirlib\.com(.*)$
RewriteCond %{REQUEST_URI} ^\/m\/pt\/(.*)$
RewriteRule ^pt(.*)$ m/pt-pt$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^(chn|are|bel|fra|ita|esp|twn|che|aus|aut|can|hkg|us|government-contractor)\.bizdirlib\.com(.*)$
RewriteCond %{REQUEST_URI} ^\/pt$
RewriteRule ^pt$ /pt-pt [L,R=301]

RewriteCond %{HTTP_HOST} ^(chn|are|bel|fra|ita|esp|twn|che|aus|aut|can|hkg|us|government-contractor)\.bizdirlib\.com(.*)$
RewriteCond %{REQUEST_URI} ^\/m\/pt$
RewriteRule ^pt$ m/pt-pt [L,R=301]

RewriteCond %{HTTP_HOST} ^(chn|are|bel|fra|ita|esp|twn|che|aus|aut|can|hkg|us|government-contractor)\.bizdirlib\.com(.*)$
RewriteCond %{REQUEST_URI} ^\/fil(.*)$
RewriteRule ^fil(.*)$ $1 [L,R=301]

RewriteCond %{HTTP_HOST} ^(chn|are|bel|fra|ita|esp|twn|che|aus|aut|can|hkg|us|government-contractor)\.bizdirlib\.com(.*)$
RewriteCond %{REQUEST_URI} ^\/m\/fil(.*)$
RewriteRule ^fil(.*)$ m/$1 [L,R=301]

#在fil語言後面增加/,要不然個别站點涉及到files的進行了錯誤的替換,導緻正常的頁面打不開。
RewriteCond %{HTTP_HOST} ^(chn|are|bel|fra|ita|esp|twn|che|aus|aut|can|hkg|us|government-contractor)\.bizdirlib\.com(.*)$
RewriteCond %{REQUEST_URI} ^\/fil\/(.*)$
RewriteRule ^fil(.*)$ $1 [L,R=301]

RewriteCond %{HTTP_HOST} ^(chn|are|bel|fra|ita|esp|twn|che|aus|aut|can|hkg|us|government-contractor)\.bizdirlib\.com(.*)$
RewriteCond %{REQUEST_URI} ^\/m\/fil\/(.*)$
RewriteRule ^fil(.*)$ m$1 [L,R=301]

#然後再單獨增加一個fil在最後的不帶/的頁面情況

RewriteCond %{HTTP_HOST} ^(chn|are|bel|fra|ita|esp|twn|che|aus|aut|can|hkg|us|government-contractor)\.bizdirlib\.com(.*)$
RewriteCond %{REQUEST_URI} ^\/fil$
RewriteRule ^fil$ $1 [L,R=301]

RewriteCond %{HTTP_HOST} ^(chn|are|bel|fra|ita|esp|twn|che|aus|aut|can|hkg|us|government-contractor)\.bizdirlib\.com(.*)$
RewriteCond %{REQUEST_URI} ^\/m\/fil$
RewriteRule ^fil$ m/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^(chn|are|bel|fra|ita|esp|twn|che|aus|aut|can|hkg|us|government-contractor)\.bizdirlib\.com(.*)$
RewriteCond %{REQUEST_URI} ^\/m\/fil$
RewriteRule ^fil$ m/$1 [L,R=301]
更改為:

RewriteCond %{HTTP_HOST} ^(chn|are|bel|fra|ita|esp|twn|che|aus|aut|can|hkg|us|government-contractor)\.bizdirlib\.com(.*)$
RewriteCond %{REQUEST_URI} ^\/m\/fil$
RewriteRule ^fil$ m [L,R=301]

發表新回應

Plain text

  • 不允許使用 HTML 標籤。
  • 自動將網址與電子郵件地址轉變為連結。
  • 自動斷行和分段。