2013-03-15 65 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

相关问题