2011-03-23 35 views
5

我想重寫以下url:htacces重寫tld沒有改變子域或目錄

子域應該匹配任何子域。 TLD也一樣。 兩個:http://car.example.com/http://cat.example.co.uk應該重寫

http://subdomain.example.com/some/dirhttp://subdomain.example.nl/some/dir

http://example.com/some/dirhttp://exampkle.nl/some/dir

(也以www ADRESS)

,但我的htaccess的知識和一般來說重寫規則對於t來說不夠好他:(

我希望你們其中一位知道解決方案。

ps。我也嘗試搜索;)

回答

3

挑戰在於具有檢測並考慮四個不同可能域圖案:

  • example.com → example.nl
  • example.co.uk → example.nl
  • sub.example.com → sub.example.nl
  • sub.example.co.uk → sub.example.nl

那麼,究竟該規則集的作用是檢查該TLD不是。 nl(防止發生循環),然後將子域(不論是否爲www)從前端拉出(讀取爲「捕獲除點之後的任何其他內容(可選)」),然後是基本域,然後是點。我們不需要匹配整個網址,因爲我們沒有保留頂級域名。

RewriteEngine On 
RewriteCond %{HTTP_HOST} !example\.nl$ 
RewriteCond %{HTTP_HOST} ^([^.]+\.)?example\. 
RewriteRule^http://%1example.nl%{REQUEST_URI} [NC,L,R=301] 

的重寫規則的^匹配任何URL,然後插入第一組在前面的RewriteCond(子域)與%1括號中的內容,並通過附加請求的路徑和標誌忽略大小寫完成重寫,將其作爲最後一條規則,並使用搜索引擎友好301重定向,以確保重寫的URL顯示在用戶的瀏覽器中。任何查詢字符串(出現在URL中的?之後的文本)默認都會自動包含。

+0

差不多!有一個無限循環,因爲如果TLD是正確的,你不會停止重寫。添加了RewriteCond%{HTTP_HOST}!^([^。] + \。)?示例\。nl - 如果TLD正確,現在停止。另外,驚人的第一個答案!歡迎來到SO – Tjirp 2011-03-23 14:38:14

+0

其實不是我在這個網站上的第一個答案 - 只是第一個被承認。 – Eric3 2011-03-23 17:06:51

2

試試這個: 編輯:看到更改子域名,使用%1從的RewriteCond

捕捉
RewriteEngine On 
# Check if the hostname requested is subdomain.example.com or empty 
# Now we attempt to capture the subdomain with (.*)? and reuse with %1 
RewriteCond %{HTTP_HOST} ^(.*)?example.com$ [NC] 
RewriteCond %{HTTP_HOST} !^$ 
# Rewrite it as subdomain.example.nl and redirect the browser 
RewriteRule ^(.*) http://%1example.nl$1 [L,R,NE,QSA] 

# Note: With the above edit for %1, this part should no longer be necessary. 
# Then do the same for example.com, with or without the www 
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC] 
RewriteCond %{HTTP_HOST} !^$ 
RewriteRule ^(.*) http://www.example.nl$1 [L,R,NE,QSA] 
+0

稍微更新了我的問題。子域名和頂級域名是「未知的」(當然,不完全是這樣,但現在在3個不同的頂級域名中有14個不同的子域名,它們都應該被重寫爲帶有相同子域名和後綴的.nl TLD)。如果這是不可能的,這是一個恥辱,我會用你的snipper很多次:) – Tjirp 2011-03-23 13:15:40

+0

看到我上面的變化。這是未經測試的,但我認爲它會起作用。 – 2011-03-23 13:23:35

+0

雖然您在子域中更改地址,但您沒有爲地址域尋址。感謝您的幫助tho :) Eric3的帖子(部分)是解決方案 – Tjirp 2011-03-23 14:37:13