2013-07-22 35 views
0

我使用htaccess的我的原單的網址是URL重寫使用htacces在我的情況

http://www.example.com/products/product_detail.php?url=pro-name 

我想要的網址會像這樣

http://www.example.com/products/pro-name 

,但我部分地以這樣的URL進行重寫我的網址使用此代碼的.htaccess

http://www.example.com/products/product_detai/pro-name 
RewriteRule product_detail/url/(.*)/ product_detail.php?url=$1 
RewriteRule /(.*) product_detail.php?url=$1 

這裏我不知道如何得到我想要的網址。請幫助任何人獲得渴望的網址。謝謝

回答

0
#Assuming the correct RewriteBase is used... 

#Redirect the client to the fancy url 
RewriteCond %{QUERY_STRING} ^url=(.*)$ 
RewriteRule ^product_detail\.php$ %1? [R,L] 

#Rewrite the url internally and stop rewriting 
#to prevent a loop 
RewriteRule ^(.*)$ product_detail.php?url=$1 [END] 

在這段代碼中,您首先將客戶端重定向到您希望在地址欄中可見的url。 %1與RewriteCond的第一個捕獲組相匹配。尾隨?清除查詢字符串。第二條規則在內部重寫URL,以便服務器可以實際生成輸出而不是404錯誤。 END標誌(可從apache 2.3.9及以上; docs),將停止重寫URL的完整)。這是爲了防止URL不斷重寫的永無止境的循環。 (docs

編輯:2.3.9以下版本的apache沒有END標誌。爲了防止循環,你需要解決這個問題。您可以使用,例如:

#Assuming the correct RewriteBase is used... 

#Redirect the client to the fancy url 
RewriteCond %{QUERY_STRING} !redirect=true 
RewriteCond %{QUERY_STRING} ^url=(.*)$ 
RewriteRule ^product_detail\.php$ %1? [R,L] 

#Rewrite the url internally and stop rewriting 
#to prevent a loop 
RewriteRule ^(.*)$ product_detail.php?url=$1&redirect=true [L] 
+0

感謝您的支持。但這段代碼給了500頁的錯誤 –

+0

我剛測試過它,並且在我的代碼中沒有語法錯誤。你很可能使用的是一個小於2.3.9的apache版本,這個版本不會有'END'標誌。你需要解決它或需要升級你的Apache版本。 – Sumurai8

1

啓用mod_rewrite的,並通過httpd.conf的.htaccess,然後把這個代碼在你.htaccessDOCUMENT_ROOT目錄:

Options +FollowSymLinks -MultiViews 
# Turn mod_rewrite on 
RewriteEngine On 
RewriteBase/

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(products)/product_detail.php\?url=([^\s]+) [NC] 
RewriteRule^/%1/%2? [R=302,L] 

RewriteRule ^(products)/(.+?)/?$ /$1/product_detail.php?url=$2 [L,NC,QSA]