Redirecting using rewrite rules from international top level domains to a language subdirectory doesn't change the domain name
I want to change from TLDs to TLD and language code.
Right now we have 4 TLDs for our webpage:
example.de
example.com
example.nl
example.fr
Now I want to change our settings of our CMS (Contao) to have only one TDL and language short terms:
example.com/de/
example.com/en/
example.com/nl/
example.com/fr/
Now if you type example.com, Contao detects the browser language and redirects to the right language short term.
Is this the right way to redirect:
RewriteCond %HTTP_HOST example.de$ [NC]
RewriteRule ^((?![a-z]2/).*)$ /de/$1 [R=301,L]
RewriteCond %HTTP_HOST example.com$ [NC]
RewriteRule ^((?![a-z]2/).*)$ /en/$1 [R=301,L]
RewriteCond %HTTP_HOST example.nl$ [NC]
RewriteRule ^((?![a-z]2/).*)$ /nl/$1 [R=301,L]
RewriteCond %HTTP_HOST example.fr$ [NC]
RewriteRule ^((?![a-z]2/).*)$ /fr/$1 [R=301,L]
Now I get redirected to example.de/de/app.php all the time.
You didn't specify the domain name in the rewrite rule destination, so it is using the current one. You should add example.com to the rewrite rule to change the domain:
RewriteCond %HTTP_HOST example.de$ [NC]
RewriteRule ^((?![a-z]2/).*)$ http://example.com/de/$1 [R=301,L]
You shouldn't need the ^(?![a-z]2/) in that rule for the country code domains. That is checking and only redirecting if there isn't a two letter subdirectory already on the URL. For that domain, there shouldn't be one anyway. So you can probably use a simpler rule:
RewriteCond %HTTP_HOST example.de$ [NC]
RewriteRule ^(.*)$ http://example.com/de/$1 [R=301,L]
You can make all your country code domains into a single rule and then use a separate special rule for .com. The .com rule will need your check that the country code doesn't already exist.
RewriteCond %HTTP_HOST example.([a-z]2)$ [NC]
RewriteRule ^(.*)$ http://example.com/%1/$1 [R=301,L]
RewriteCond %HTTP_HOST example.com$ [NC]
RewriteRule ^((?![a-z]2/).*)$ /en/$1 [R=301,L]
With those two rules you should be all set. When you are testing, be sure to clear your browser cache each time you test. Otherwise, previous incorrect redirects could get cached and give you redirect loops.
Comments
Post a Comment