Geo Location Based Redirects with CloudFront and Apache

I want to change from TLDs to TLD and language code. Right now we have 4 TLDs for our webpage: 

Now I want to change our settings of our CMS (Contao) to have only one TDL and language short terms: 

Now if you type, 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 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 to the rewrite rule to change the domain:

RewriteCond %{HTTP_HOST} example\.de$ [NC] RewriteRule ^((?![a-z]{2}/).*)$$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 ^(.*)$$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 ^(.*)$$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: always
  • 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?
  • @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?
  • 1 That 'redirect loop' is probably because 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.
  • 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.

fungerat för dig: Charles Robertson | Vill du kontakta oss?

Användbar information