2013-03-15 64 views
3

這是我想做的事:mod_rewrite的與子域下劃線替換破折號然後重定向

  • 我想要替換所有破折號 - 用下劃線(_),但只有在子域()。
  • 畢竟破折號代替我想重定向到一個子目錄與重寫的子域

例如名稱: http://subdomain-with-dashes.rotarytest.de/a-directory/an-image.png 重寫後應 http://rotarytest.de/subdomain_with_dashes/a-directory/an-image.png

這裏是我的權利現在,請參閱代碼中的註釋

RewriteEngine on 

# replace dashes with underscores 
# this works, but only for the last dash 
RewriteCond %{HTTP_HOST} ^(.*)-(.*)$ 
RewriteRule ^(.*)$ http://%1_%2/$1 [L,R=301] 


# if a subdomain is called, redirect to subdirectory 
# this code works but only when i have one dash in my subdomain 
RewriteCond %{HTTP_HOST} ^(.*)\.rotarytest\.de$ 
RewriteRule ^(.*)$ http://rotarytest.de/%1%{REQUEST_URI} [L,R=301] 

我嘗試了幾乎所有我在這裏找到的解決方案stackoverflow或網絡上,但他們都沒有正常工作。

有人可以幫我嗎?先謝謝你。

+0

這是什麼意思,當規則3只是將該子域重定向到mydomain.de/subdomain-name? – Gerben 2013-03-15 14:17:58

+0

正如你可以在我的評論中看到的,我首先想用下劃線替換破折號,然後將重寫的子域重定向到一個文件夾:http://mydomain.de/subdomain_previously_with_dashes/a-directory/an-image.png – jmartsch 2013-03-18 11:06:12

回答

0

您必須允許瀏覽器不斷重定向以刪除破折號。如果你不需要用URI中的下劃線替換破折號,那麼你不需要前兩個規則,它們不會被應用到主機名。用這個替換前兩條規則:

RewriteCond %{HTTP_HOST} ^(.*)-(.*)$ 
RewriteRule ^(.*)$ http://%1_%2/$1 [L,R=301] 
+0

這隻能部分起作用。破折號被替換爲下劃線,但我的以下規則應該應用於該重寫的URI,以便獲得http://mydomain.de/subdomain_previously_with_dashes/a-directory/an-image.png。所以我刪除了你的規則上的L標籤,但是破折號不會被替換。 – jmartsch 2013-03-18 07:44:19

相關問題