2012-09-21 72 views
1

在我的網站中,有三到四種類型的URL(每個可以有不同的參數來表示不同的頁面)。他們就像http://foo.com/users/usernamehttp://foo.com/questions/question_texthttp://foo.com/new_questionMOD_Rewrite指令實現一個重定向

我用mod_rewrite重寫URL從客戶端來了,即http://foo.com/users/username變得http://foo.com/users?username=username,那麼這個請求到達http://foo.com/users,這沒有休息。

現在我想要的是,除了這些3-4個預定義的模板之外,如果有其他請求,如http://foo.com/abracadabra,用戶應該被重定向到主頁http://foo.com/。有沒有這樣的指令可以實現這種重定向?

回答

1

一個很長的路將是:

RewriteEngine On 
# If the request isn't actually a file 
RewriteCond %{REQUEST_FILENAME} !-f 
# If the request isn't actually a directory 
RewriteCond %{REQUEST_FILENAME} !-d 
# If the request isn't specifically for favicon.ico (though the first 
# RewriteCond should take care of this, it's in the first .htaccess file 
# I grabbed to verify) 
RewriteCond %{REQUEST_URI} !=/favicon.ico 
# Change the request from whatever the user entered to "/" 
RewriteRule ^(.*)$/

但你真的不應該這樣做。如果您請求的頁面不存在,實際上會提供404錯誤和自定義錯誤頁面,這非常友好。另外,如果您這樣做並且您的站點必須通過第三方安全掃描,那麼您將爲報告產生大量誤報安全漏洞。

+0

嗯,那是一口!你能解釋一下你剛剛寫的是什麼,我無法辨別出它的一個分數。 – SexyBeast

+0

當然,後編輯。 – Crontab

+0

謝謝。通過'REQUEST_FILENAME',你的意思是我提到的基礎PHP文件,比如'users','questions'等等?再次,你能否詳細說明你最後的**誤報**位? (請不要惱火!) – SexyBeast