I have two domains and one valid subdomain. How can i achieve this?

ex-ample.com    » example.com
www.example.com » example.com
en.ex-ample.com » en.example.com
en.example.com  » en.example.com

I'm using the following .htaccess rule currently:

RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

Tried many but the best i can get is a redirect loop. Can you help me?

link|improve this question

80% accept rate
If you want to further debug you rewriterule configuration, enable your rewritelog (using RewriteLog and RewriteLogLevel) and see exactly what's happening (and maybe post the log entries here). – larsks Jun 23 '11 at 13:19
feedback

2 Answers

up vote 3 down vote accepted

If I have correctly understood your request, I think a typical named virtual host configuration will get you what you want. I'm not entirely sure what you mean by "save virtualhost"; if this solution isn't helpful perhaps you can elaborate on your request.

In any case, I was envisioning something like this:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName example.com
    ServerAlias ex-ample.com www.example.com
    DocumentRoot /path/to/example.com
</VirtualHost>

<VirtualHost *:80>
    ServerName en.example.com
    ServerAlias en.ex-ample.com
    DocumentRoot /path/to/en.example.com
</VirtualHost>

Now requests for any of...

...will go to the example.com VirtualHost, and requests for...

...will go to the en.example.com VirtualHost.

link|improve this answer
sorry but this won't redirect just serving the site. What i need is a proper 301 redirect because of SEO. – fabrik Jun 23 '11 at 12:53
So put a Redirect rule inside every VirtualHost other than the "main" one. – larsks Jun 23 '11 at 13:18
Your comment was the solution, thank you! – fabrik Jun 23 '11 at 13:33
feedback

If all you're doing is looking for a rewrite rule, try this:

# REWRITE en.ex-ample.com to en.example.com
RewriteCond %{HTTP_HOST} ^en\.ex-ample\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$    
RewriteRule (.*) http://en.example.com$1 [R=301,L]

# REWRITE ANYTHING OTHER THAN 'example.com' and 'en.example.com'
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^en\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$    
RewriteRule (.*) http://example.com$1 [R=301,L]

Assuming you have one virtualhost for all appropriate domains.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.