2015-06-29 31 views
2

我想獲得一些有關此URL重寫的幫助。我已經閱讀了多篇關於如何完成這些工作的教程和文檔頁面,但沒有一本對我有意義。我也不懂正則表達式,所以也沒有幫助。我有一個半工作的代碼,只需要幫助讓它正常工作。URL使用變量將變量重寫爲常規域的子域

我需要:http://subdomain.domain.com?dl=2

重定向到http://domain.com/subdomain.php?dl=2

我的代碼是

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^subdomain.example\.com$ [NC] 
RewriteCond %{REQUEST_URI} !page.php 
RewriteRule ^(.+/)?([^/]*)$ page.php?dl=$2 [QSA,L,NC] 

它發送的變量,但無法弄清楚的子域的一部分。如果任何人可以請幫助我,這將不勝感激。

回答

0

您需要檢查QUERY_STRING作爲RewriteRule不包括它。另外,您的規則不使用重定向標誌R

RewriteEngine on 

# First, check for the subdomain 
RewriteCond %{HTTP_HOST} ^subdomain.domain.com$ [NC] 

# Then, check the query string - it should match digits (\d+) 
RewriteCond %{QUERY_STRING} ^dl=\d+ [NC] 

# Check if we are not at subdomain.php 
# (This is redundant, but leaving it here in case you really need it) 
RewriteCond %{REQUEST_URI} !^/subdomain.php 

# If all the above conditions are true, match a root-request 
# and redirect to domain.com/subdomain.php with the query string 
# Note: You don't need to actually specify the query string 
#  in the destination URI - Apache will automatically 
#  hand it over upon redirect (using the R flag). 
#  The only time this is not the case is when you 
#  either add the QSD flag, or append the destination 
#  URI with a question mark. 
RewriteRule ^$ http://domain.com/subdomain.php [R=302,L] 

上面將重定向http://subdomain.domain.com/?dl=2http://domain.com/subdomain.php?dl=2。如果您想通過瀏覽器和搜索引擎將重定向永久化並緩存,請將302更改爲301

+1

謝謝你,正是需要什麼和一個很好的解釋。 –

+0

非常歡迎。 –