2012-01-19 213 views
0

我已經做了一些搜索,並且我找不到解決方案。這是我想要的。用戶類型:如何隱藏查詢字符串,並使用htaccess重寫URL

Case 1: www.example.com/user/profile.php?alias=bob 
Case 2: www.example.com/user/bob 

雙方應在用戶的瀏覽器顯示爲:

www.example.com/user/bob 

內部,應該將 「profile.php別名=鮑勃?」

目前,我在我的htaccess文件中有以下mod_rewrite規則:

RewriteEngine On 
#Converting alias to query 
RewriteRule ^([A-Za-z0-9-]+)/?$ profile.php?alias=$1 [QSA,L] 

這對於案例2有效。對於案例1 ho wever,url仍然顯示完整的查詢。我如何才能讓它在案例1中正確顯示?

回答

0

嘗試將以下內容添加到您網站根目錄下的htaccess文件中。

RewriteEngine On 
RewriteBase/

#if the request is using profile.php 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /user/profile\.php\?alias=([^&\ ]+) [NC] 
#redirect to user/[ALIAS] 
RewriteRule . /user/%1 [L,R=301] 

#extract alias from /user/alias 
RewriteRule ^user/([A-Za-z0-9-]+)/?$ profile.php?alias=$1 [QSA,L] 
+0

沒有太大的工作。還打破了案件2 ...嗯.... – Skyd

+0

@Skyd編輯放在profile.php。讓我知道您測試過的網址,如果不正確,請告訴我結果。 –

相關問題