URL Rewriting Help


I have a litte problem wir rewriting my URL correctly. The starting point is following:

I have one site, which is accessible under 3 domain names -
domain.com - English version
domain.de - German version
domain.fr - French version

I always want the domains only to be accessed as www.domain.com/de/fr.
And I also want to allow images.domain.com/de/fr (for an image download)

But for some reason, we are sometimes listed with some subdomain name, which actually does not exist like events.domain.com oder www.events.domain.com. How do I have to modify my .htaccess file to limit the subdomain access to images.domain.com/de/fr and rewrite all wrong urls to www.domain.com/de/fr?

What I have so far is:

##### www Umleitung #####
## .com
RewriteCond %{HTTP_HOST} ^domain.com$ [NC] 
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301] 
## .net
RewriteCond %{HTTP_HOST} ^domain.net$ [NC] 
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301] 
## .de
RewriteCond %{HTTP_HOST} ^domain.de$ [NC] 
RewriteRule ^(.*)$ http://www.domain.de/$1 [L,R=301] 

## .fr
RewriteCond %{HTTP_HOST} ^domain.fr$ [NC] 
RewriteRule ^(.*)$ http://www.domain.fr/$1 [L,R=301] 

Thanks for your help!


Okay, force .com/language for non-English languages (specifically German and French) AND eliminate subdomains which are not merely www (redirect to www’d domain name).

The red code above is definitely a problem as you’re not redirecting to the .com domain name. Additionally, your {HTTP_HOST} start anchors are not matching www, are not matching www.events, etc. So, in pseudo code:

# deal with subdomains
Test all SUBdomain names and redirect if not simply www

# deal with net as the oddball
Do NOT test the .com domains
Test the .net domain name and redirect to .com

# deal with de and fr domains (don't worry about www - that was in the first step)

Coding that is simply:

# deal with subdomains
RewriteCond %{HTTP_HOST} ^domain\\. [NC]
RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# using regex .? because the {REQUEST_URI} is already available
# you have to retain the host to keep the de/fr languages

# deal with net as the oddball
RewriteCond %{HTTP_HOST} domain\\.net [NC]
RewriteRule .? http://www.domain.com%{REQUEST_URI} [L,R=301]

# deal with de and fr domains (don't worry about www - that was in the first step)
RewriteCond %{HTTP_HOST} domain\\.(de|fr) [NC]
RewriteRule .? http://www.domain.com/%1%{REQUEST_URI} [L,R=301]



Hmm, if I request a domain “test.domain.de” is does not change to “www.domain.de” So the first points seems not to work, while I don’t know why. Everything looks logical.

The de|fr rewriting looks wrong to me since the de is added to an existing com. I guess it would need to be

RewriteCond %{HTTP_HOST} domain\\.(de|fr) [NC]
RewriteRule .? http://www.domain.[COLOR=#FF0000]%1[/COLOR]%{REQUEST_URI} [L,R=301]

But somehow the whole construction does work as I expected. Hmm either there is some error in the code here or I need to check is more detailed on the server.

Thanks so far.

If everything needs to be redirected to www and you have no other subdomains whatsoever you could use

RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Although I think I would personally prefer to just 404 anything that doesn’t exist, instead of sending all kinds of non-existent sub-domains to www.
I’d just keep [noparse]domain.com/de/fr[/noparse] and [noparse]www.domain.com/de/fr[/noparse] in the DNS and nothing else.


text.domain.de does not work as your statement of the problem was that you merely wanted to force www on domain.x. If you don’t ware about any previous subdomain, merely remove the ^ in the first RewriteCond statement. HOWEVER, I believe that would still eliminate the test subdomain when the fr or de domain extension is removed in the last mod_rewrite block and you would never see any test.domain.de redirection except to www.domain.com/de.

If this does not do the trick for you, please restate your problem specification so nothing is missed. Please remember that “Specificity” is the most important part of coding mod_rewrite as only then do you know what you want it to do (then the coding is a simple matter of translation).