2015-12-30 192 views
1

我試圖路由一個url,顯示如下www.example.com/profile/1到以下頁面,參數爲www.example.com/profile?user=1。我的.htaccess中有以下RewriteRule,但路由不起作用。有人能告訴我我做錯了什麼嗎?使用.htaccess路由友好的網址

RewriteEngineOn 
RewriteBase/
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/profile/$1 [R=301,L] 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule (.+) /profile?user=$1 [L,QSA] 

我試過將最後的RewriteRule切換到不同的URL,但到目前爲止沒有運氣。我可能會監督某個人可以幫助我的小問題。

回答

1

你需要允許profile/\w+爲正則表達式匹配/profile/123

Options -MultiViews 
RewriteEngineOn 
RewriteBase/

RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteRule^http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^profile/(\w+)/?$ /profile?user=$1 [L,QSA,NC]