3

Here is what i want to do:

  • I want to replace all dashes (-) with underscores (_) but only in subdomains.
  • After all dashes are replaced i want to redirect into a subdirectory with the name of the rewritten subdomain

For example: http://subdomain-with-dashes.rotarytest.de/a-directory/an-image.png after rewrite should be http://rotarytest.de/subdomain_with_dashes/a-directory/an-image.png

Here is what i have right now, please see comments in code

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]

I tried almost every solution i found here on stackoverflow or on the web, but none of them worked correctly.

Can someone help me out? Thank you in advance.

4

1 回答 1

0

您必须允许浏览器不断重定向以删除破折号。如果您不需要将破折号替换为 URI 中的下划线,那么您不需要前两个规则,它们不会应用于主机名。将前两条规则替换为:

RewriteCond %{HTTP_HOST} ^(.*)-(.*)$
RewriteRule ^(.*)$ http://%1_%2/$1 [L,R=301]
于 2013-03-15T22:09:06.637 回答