2

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.

1

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.

  • The problem is, it still redirects to: dev.example.com/en/app.php always – Lydia Maier May 3 at 14:15
  • 1
    You could reduce the separate rules for each language into a single rule.... by capturing the TLD (eg. example\.([a-z]{2,3})$) and using a %1 backreference in the substitution. Although that doesn't work for the .com domain - you could just make that a special case and group the rest? – MrWhite May 3 at 14:28
  • @LydiaMaier What URL are you requesting? What other directives do you have in your .htaccess file? If app.php is your "front-controller" then you may have put these directives in the wrong place or you have a conflict? – MrWhite May 3 at 14:30
  • 1
    That "redirect loop" is probably because example.com is used both as its own language redirect (en) and the target for all other languages. I would probably just make .com a special case before your existing redirects, in which you check that no language code has been specified. There shouldn't be any need to check for the presence of the language code in the other "TLD specific" redirects, as @Stephen has pointed out. – MrWhite May 3 at 14:45
  • 1
    I've edited @MrWhite 's suggestions into this answer and given you another suggestion about how to deal with redirect loops. I don't think the order of the rules matter because the conditions for domains don't overlap. – Stephen Ostermiller May 3 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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