Redirecting using rewrite rules from international top level domains to a language subdirectory doesn't change the domain name

Redirecting using rewrite rules from international top level domains to a language subdirectory doesn't change the domain name - If a page has internal and external outgoing links to redirecting URLs, it’s returning 3xx (301, 302, etc.) HTTP status codes standing for redirection. This issue means that the page does not exist on a permanent or temporary basis. It appears on most of the popular web browsers, usually caused by a misconfigured website. However, there are some steps you can take to ensure the issue isn’t on your side. You can find more details about redirecting URLs by reading the Google Search Central overview. In this article, we’ll go over how you can fix the Redirecting using rewrite rules from international top level domains to a language subdirectory doesn't change the domain name error on your web browser. Problem :


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.


Solution :

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.


We hope that this article has helped you resolve the htaccess, redirects, apache error in your web browsers. Enjoy browsing the internet uninterrupted!

Comments

Popular posts from this blog

How to redirect to any domain [duplicate]

"302 found" for index page bad for SEO?

Create redirect from url like www.example.us/?p=100&option=