2012-01-05 54 views
0

你怎麼會重定向(301?)?:如何查詢變量設置成子目錄的.htaccess(301重定向)

test.com/?dep=CAR(變量是動態的,以後無論發生什麼事=)

到子目錄:

test.com/CAR/

這是我到目前爲止有:

Options +FollowSymlinks 
RewriteEngine on 

RewriteCond %{QUERY_STRING} ^dep=(.*) 
RewriteRule ^.*/[L,R=301] 

我在localhost(WAMP)上測試這個。
和主目錄是本地主機/ newsite/
(本地主機/ newsite /?DEP = CAR)
,但我想這不應該的問題?在線或本地主機。

回答

1

請嘗試以下

Options +FollowSymlinks 
RewriteEngine on 
RewriteBase/

#if it has a dep param, capture the value 
RewriteCond %{QUERY_STRING} (^|&)dep=([^&]+) [NC] 
#and redirect to /value/ 
RewriteRule .* /%2/? [L,R=301] 


#if there is no dep param 
RewriteCond %{QUERY_STRING} !dep= [NC] 
#and capture the directory and pass as a dep param 
RewriteRule ^([a-zA-Z]+)/$ ?dep=$1 [L] 
+0

當我嘗試這個,我得到的「http://本地主機// DEP = CAR」(我已經更新了更多的信息運算) – Rob 2012-01-05 22:22:08

+0

@Rob固定錯字並添加'?'來抑制原始查詢字符串 – 2012-01-05 22:39:35

+0

url到達子目錄,但是說的是「在此服務器上找不到請求的URL localhost/newsite/CAR /」。 org localhost/newsite /?dep = CAR。 – Rob 2012-01-05 22:53:11