.htaccess rewrite based on browser language apply only once - .htaccess

I have the following rewrite rules to redirect a visitor to the correct language page:
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ /en/[L,R=301]
RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^$ /fr/[L,R=301]
RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteRule ^$ /es/[L,R=301]
This code works fine but has a drawback.
The site is Dutch by default and hasn't a specific directory to point to except for the root domain.
If you're Dutch but your browser language is setup to be English you will be redirected to root.com/en/ which is expected behaviour.
However the site has three language links to point you to the other translations of that page. These links are all working fine except the one linking to the root domain (the Dutch homepage)
The link to the Dutch homepage keeps redirecting you to the /en/ when you're browser language is set to English.
This only happens at the homepage which is obvious. However it there a way to apply these rewrite rules only once so you can override the redirect if you want to?
With the current setup you can never reach the homepage / root domain after being redirected.
Thanks.

Related

htaccess redirect url not working /?index.html to /

I have a client project on search engine marketing with a website https://www.iehsacademy.com/,
The site is 13 years old. before the client was using the URL structure like with HTML like this: https://www.example.com/?index.html (Home Page),
https://www.example.com/?cndc.html,
But last year they completely remake the website URL structure and make it to https://www.example.com/ (Home Page), https://www.exsample.com/about. https://www.example.com/contact.
Now, The old URLs and the new URLs are both working. The client wants and also for SEO purposes we want to block the old URL or redirect them to the new URL. I tried many rewrite conditions on htaccess
RewriteRule ^([a-z]+).html https://www.iehsacademy.com/ [QSA,L]
This one also
RewriteCond %{HTTP_HOST} ^www.iehsacademy.com/?cndc.html$
RewriteRule (.*)$ http://www.iehsacademy.com/$1 [R=301,L]
Not Working anything. The client's developer is also confused. Can anyone help with this or where i am doing wrong?
NB: I took permission from the site owner to share the client's URLs and problems.
You can use the following redirection rule :
RewriteEngine On
#redirect /?index.html to /
RewriteCond %{HTTP_HOST} ^www.iehsacademy.com$ [NC]
RewriteCond %{QUERY_STRING} ^index\.html$
RewriteRule ^$ https://example.com/? [L,R=301]
#redirect /?foobar to /foobar
RewriteCond %{HTTP_HOST} ^www.iehsacademy.com$ [NC]
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^$ https://example.com/%1? [L,R=301]
Make sure to clear your browser caches or use a different browser to test the redirection. And do not forget to replace the destination domain example.com with your domain name.

Redirect by browser language in htaccess but only from external urls

Is it possible to redirect due to browser language but then give users the possibility to change language on the website via navigation menue link?
I tried this but this changes the language of the website according to the browser language but then it is not possible to use the flag/navigation on the website anymore.
# language starts with DE
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^$ /de/ [L,R=301]
# Fallback to default Language (en)
RewriteCond %{HTTP:Accept-Language} !^.*(de|en).*$ [NC]
RewriteRule ^$ http://%{HTTP_HOST}/ [R,L]
I thought about the same htaccess code but with the addon: "only for external visitors" so users which are already on this domain, can use the flag/navigation menue to change language and wont redirected anymore by the htaccess.
Is it possible to do that via htaccess?
Thanks!

Forward no-www to www including the sub-directories "/country-code"

We have a website "www.testa-omega3.com" with 3 different languages (DE, EN, NL).
Our default language is Dutch (NL), which is currently the main domain.
The DE and EN language are in a sub-directory:
www.testa-omega3.com/de/
www.testa-omega3.com/en/
When we enter the full url's in the browser it will show the correct page. The non-www for the DE and EN is not redirecting correctly.
These:
testa-omega3.com/en
testa-omega3.com/en/
testa-omega3.com/de
testa-omega3.com/de/
Are all redirecting to "www.testa-omega3.com"
Currently we have the following lines in our htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
What are we doing wrong?
It seems your (sub)domain matching is set up incorrectly. Your rewrite rule is based on the URL already being entered with the www subdomain when you actually want to match against the naked domain.
Changing the line
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
to
RewriteCond %{HTTP_HOST} ^domain\.com$
should work.
(Your rewrite condition doesn't work at all on my end btw.; the server I'm hosted with complains that it gets stuck in a loop.)
In my rewrite rule I couldn't get the {HTTP_HOST} variable to work the way you (re)used it, but using the full domain name worked just fine:
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

How to apply a htaccess only when arriving on the website?

Let me explain the problem. We have a website in 2 languages: fr & nl (dutch).
When you arrive on our website, you land on www.domain.be which redirects
you (as you can see in the following code) to the dutch version if your browser language
is set to 'nl' and if it's set to 'en' (because dutch people often use this language for they browser) or leave you on www.domain.be / fr.domain.be (both url are work to call the website - the fr... one is more in response of the nl... one)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} ^nl [NC]
RewriteRule ^$ http://nl.domain.be/ [L,R]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ http://nl.domain.be/ [L,R]
</IfModule>
On the website, you can choose, by clicking on 2 links (in the top right corner), if you want to go to fr.domain.be or nl.domain.be.
When you click on one of those link, the htaccess redirects you even if you want to go to the fr part (while navigating on the dutch one) and the same on the nl part.
How can I solve that? I would like the htaccess to only redirect you when you first come to the website but then be desactivated and allow the user to choose his language if he wants to.
Could you please help me? I'm on this for like two days...
You're only redirecting the site base /, so swith directly to another page should not be a problem.
Can't you just use a fake index page when you manually switch ? Like http://nl.domain.be/index
You can create multiple conditions for a rule set so you could add an aditional check that the redirect only happens when you do not match the HTTP_REFERER to your domains.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} ^nl [NC]
RewriteCond %{HTTP_REFERER} !^*\.domain\.be/ [NC]
RewriteRule ^$ http://nl.domain.be/ [L,R]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteCond %{HTTP_REFERER} !^*\.domain\.be/ [NC]
RewriteRule ^$ http://nl.domain.be/ [L,R]
</IfModule>
the ! means NOT so basically you are saying when the HTTP_REFERER does NOT match the url pattern, which is "(wildcard).domain.be".
This will prevent the rule from being run if they are on your site currently and trying to change the language.
NOTE: I'm not near an apache box to test this so my syntax might be off but that should get you down the right path.

RewriteCond HTTP:Accept-Language all subfolders

Currently I have added the following code to the .htaccess. This code detects browser language and forwards the Spanish visitors to /es/
When users with a Spanish language visit mysite.com they are automatically send to mysite.com/es/
RewriteEngine On
RewriteCond %{HTTP:Accept-Language} ^(es.*) [NC]
RewriteCond %{REQUEST_URI} !(^/images/.*)
RewriteCond %{REQUEST_URI} !(^/es/.*) [NC]
RewriteRule ^(.*)$ /es/$1 [L,R=301]
This works fine, but does not work with subfolders. For example if the same user visits mysite.com/page/ they are not send to mysite.com/es/page/
How can the code be change so that is works with all subfolders?
Like it is possible with www redirects
rewriteCond %{HTTP_HOST} !site.com
rewriteRule ^(.*)$ site.com/$1 [R=301,L]
if you visit site.com/page/test/ you will be automatically redirected to www.site.com/page/test/ Yet something like this does not seem to work with language to go from www.site.com/page/test/ to www.site.com/es/page/test/
If your layout is such that there are subfolders with content, not a web application, then you would need such a .htaccess file in each of the subfolders. The same file placed in the es directory should do the trick.
As for differentiating between Spain-Spanish and Peru-Spanish, you can try es-ES and es-PE.

Resources