2016-07-02 16 views
0

我工作的一個項目的查詢字符串的URL的.htaccess重寫規則需要幫助,我堅持這是不工作&我試過很多東西,但沒有運氣有些.htaccess重寫規則,所以請幫助我。關於與動態子目錄

下面是我在做什麼:

當我打開:domain.com/us/search.html?keyword=xyz&location=alabama

它應該有從根目錄下讀取文件search.php &工作是這樣的:search.php?directory=us&keyword=xyz&location=alabama

它的正確越來越子目錄,但沒有獲取關鍵字和位置數據。

這裏是我的.htaccess代碼:

Options +FollowSymlinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^domain.com [NC] 
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301] 
RewriteRule ^(.*)/(.*)-ad(.*).html ad.php?countrycode=$1&title=$2&id=$3 [NC] 
RewriteRule ^(.*)/$ country.php?countrycode=$1 [NC] 
RewriteRule ^(.*)/(.*)-author\.html author.php?countrycode=$1&q=$2 [NC] 
RewriteRule ^(.*)/(.*)-author/currentpage=(.*) author.php?countrycode=$1&q=$2&currentpage=$3 [NC] 
RewriteRule ^(.*)/search\.html?keyword=(.*)&location=(.*) search.php?countrycode=$1&keyword=$2&location=$3 [NC,L] 
RewriteRule ^(.*)/search\.html$ search.php?countrycode=$1 [NC,L] 
RewriteRule ^(.*)/latest\.html$ latest.php?countrycode=$1 [NC,L] 
RewriteRule ^(.*)/submit\.html$ submit.php [NC,L] 
RewriteRule ^error\.html$ error.php [NC,L] 
ErrorDocument 400 /error.html 
ErrorDocument 401 /error.html 
ErrorDocument 403 /error.html 
ErrorDocument 404 /error.html 
ErrorDocument 500 /error.html 

回答

1

相關的規則是

RewriteRule ^(.*)/search\.html$ search.php?countrycode=$1 [NC,L] 

通常RewriteRule保持現有的查詢字符串,除非你添加一個自己

修改查詢字符串

默認情況下,查詢字符串通過不變。但是,您可以在包含查詢字符串部分的替換字符串中創建URL。只需在替換字符串中使用問號表示應將以下文本重新注入查詢字符串。當您要刪除現有的查詢字符串時,只需使用問號結束替換字符串。要合併新舊查詢字符串,請使用[QSA]標誌。

保留以前的查詢字符串,你必須添加QSA|qsappend標誌

RewriteRule ^(.*)/search\.html$ search.php?countrycode=$1 [NC,L,QSA] 

這改寫search.html無條件。


如果你想只有當請求包含的查詢字符串keyword=xyz&location=alabama重寫它,你必須用%{QUERY_STRING}

RewriteCond %{QUERY_STRING} keyword=.+?&location=. 
RewriteRule ^(.*)/search\.html$ search.php?countrycode=$1 [NC,L,QSA] 

附:前面加上RewriteCond規則在開始時,你寫search.php?directory=us&...,但在規則中你有search.php?countrycode=...。相應地調整規則。

0

這裏工作.htaccess

Options +FollowSymlinks 

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^domain.com [NC] 

RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301] 
RewriteRule ^(.*)/(.*)-ad(.*).html ad.php?countrycode=$1&title=$2&id=$3 [NC] 
RewriteRule ^(.*)/$ country.php?countrycode=$1 [NC] 
RewriteRule ^(.*)/(.*)-author\.html author.php?countrycode=$1&q=$2 [NC] 
RewriteRule ^(.*)/(.*)-author/currentpage=(.*) author.php?countrycode=$1&q=$2&currentpage=$3 [NC] 

RewriteCond %{QUERY_STRING} keyword=(.*)&location=(.*) 
RewriteRule ^(.*)/search\.html search.php?countrycode=$1&keyword=%1&location=%2 [NC,L] 

RewriteRule ^(.*)/search\.html$ search.php?countrycode=$1 [NC,L] 
RewriteRule ^(.*)/latest\.html$ latest.php?countrycode=$1 [NC,L] 
RewriteRule ^(.*)/submit\.html$ submit.php [NC,L] 
RewriteRule ^error\.html$ error.php [NC,L] 

ErrorDocument 400 /error.html 
ErrorDocument 401 /error.html 
ErrorDocument 403 /error.html 
ErrorDocument 404 /error.html 
ErrorDocument 500 /error.html 

RewriteRule指令只適用於請求路徑,即只對?跡象。在?之後的所有符號都可以通過RewriteCond和它的變量%{QUERY_STRING}進行檢查。 這裏是什麼,你可以在mod_rewrite爲改變RewriteRule + RewriteCond的日誌中看到:

[rewrite:trace3] applying pattern '^(.*)/search\\.html' to uri 'us/search.html' 
[rewrite:trace4] RewriteCond: input='keyword=xyz&location=alabama' pattern='keyword=(.*)&location=(.*)' => matched 
[rewrite:trace2] rewrite 'us/search.html' -> 'search.php?countrycode=us&keyword=xyz&location=alabama' 

以及向匹配RewriteCond模式的反向引用您必須使用%N$N

+0

添加了此功能並立即開始工作,感謝您的幫助。 'RewriteRule ^(。*)/ search \ .html $ search.php?countrycode = $ 1 [NC,L,QSA]' –

+0

那麼,爲什麼你不接受正確的答案呢? – spirit

+0

完成了,我接受了正確答案 –