Language redirect using .htaccess - .htaccess

I have two versions of the same page:
example.com
example.com/fr -> French version
I would like French people who access to mydomain.com to be redirected to mydomain.com/fr based on the header Accept-language. To do that I wrote this into my .htaccess:
RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^$ http://example.com/fr/ [L,R=301]
I works perfect so far.
But in my site there are links to the French / English versions... When a French user who is in the French version clicks on the english link, he comes back to the french version... How can I avoid this? How can I distinguish he actually wants to go to the English version?

A Suggestion may be to include links to the top of your page - each to different pages, for example; FR - mydomain.com/fr, ENG - mydomain.com/eng
Depending on the context of the website - I would then set either the French or English versions of the website as the default.

You can add a parameter on the query string to the internal language links, to prevent the normal language redirection from happening when the user has clicked a link to a specific language.
Then add an extra RewriteCond to your .htaccess to check whether the user has clicked an internal link:
RewriteCond %{QUERY_STRING} !(nolangredirect=1)
So if a French user clicks on www.example.com/en/index.html?nolangredirect=1 they will not be redirected to the French version.
If you want them to see the English version next time they come back to the website, you'll need to use cookies.

Related

Redirect .htaccess based on browser language and utm parameter

I'm looking for a way in .htaccess to redirect visits coming from GMB, if two conditions match:
URL contains the parameter ?utm_source=gmb
AN example:
https://example.com/?utm_source=gmb&utm_medium=organic&utm_content=listing&utm_campaign=1
browser language is Portuguese (pt, pt-pt or pt-br)
Then to redirect to the Portuguese page is only needed to add &lang=pt-pt at the end of the URL since the site is using WPML with language as a parameter.
All the other requests, without ?utm_source=gmb, I prefer to not redirect, to avoid indexation problems on SERPS.
You can do something like the following in .htaccess using mod_rewrite, however, note the caveats that follow:
RewriteEngine On
# Check for "utm_source=gmb" anywhere in the query string
RewriteCond %{QUERY_STRING} (?:^|&)utm_source=gmb(?:$|&)
RewriteCond %{HTTP:Accept-Language} ^pt(|-pt|-br)
RewriteRule (.*) /$1?lang=pt-pt [QSA,R,L]
add &lang=pt-pt at the end of the URL
Note that this adds lang=pt-pt at the start of the query string and the remaining query string is appended (QSA flag). The order shouldn't matter, but this can be added at the end if you really require it (although it is not the "natural way" to do it).
However, this has a number of caveats regarding the language detection:
It is not possible to determine the language "accurately" here.
This assumes that the "primary" language is defined first in the Accept-Language HTTP header. Whilst this is common, this is not guaranteed.
This doesn't tell you what language the user is actually using, just that pt is an accepted language by the browser. There is no way to determine the "actual" language being used here.
Consequently, "redirecting" the user based on (basic) language detection is generally a bad idea.

Redirecting a page to 410 when a more global redirect is taking precedence

We've got a couple redirects in place sending everything inside member.php (member profiles) to the forum home page:
RewriteRule ^member.php/(.*)$ https://www.domain.com/interact/ [L,R=301,NC]
RewriteRule ^member.php$ https://www.domain.com/interact/ [L,R=301,NC]
Unfortunately we need a specific member profile to be 410 Gone due to some serious issues with Google rankings (long story - spammy backlinks to it):
https://www.domain.com/interact/member.php/82683-Donaldpab
The first set of redirects is causing the Donaldpab profile page to forward you to the forum home page instead of show it as 410.
We had this redirect in place for it, but its being ignored:
Redirect 410 /interact/member.php/82683-Donaldpab
Tried this one also:
RewriteRule ^interact/member.php/82683-Donaldpab$ - [G,L]
I'm not a developer/tech person so I'm randomly guessing.
Is there a way to retain the first set of redirects, and just make this one user profile go 410 Gone?
Thank you.
The following redirect solved the problem. It allowed the overall "rules" for member profiles to remain in effect, while 410'ing the specific member profile:
RewriteRule ^member\.php/82683-Donaldpab$ - [G,NC]
Tested and confirmed with multiple profiles.

How to 301 redirect from one language to default language

I´ve set up a prestashop and have activated two languages Danish and English. The English version will not be used for a while, but I want search engines to cache the domain.com/webshop/da/ from the beginning. I didn´t know how to add the /da/ with .htaccess.
The problem is that even if robots.txt exclude the /en/ peoples browser language (en) will put prestashop into /en/.
I would like to redirect the domain.dk/webshop/en/[products] to domain.dk/webshop/da/[products]
So customers do not land on the English version that is not translated.
How can I do that?
Or do you see a better solution for me?
Have a nice day. Best T
I would like to redirect the domain.dk/webshop/en/[products] to domain.dk/webshop/da/[products]
You can use this rule as your very first rule in /webshop/.htaccess:
RewriteEngine On
RewriteRule ^en/(.*)$ /da/$1 [L,NC,NE,R=301]

mod_rewrite caused drop my ranking

I'm owner of http://myisfahan.com. I have an article management system there that developed by myself.
Recently I changed article links from myisfahan.com/articles/isfahan_articleXXX.html to myisfahan.com/XXX_[article subject].html in newer version. Many of my articles had high ranking on Google (in Persian language).
Because I didn't know redirect older links to newer one I only wrote 2 instruction only in the .htaccess:
RewriteRule ^([0-9]+)_([^/\.]+).html$ maghalat.php?id=$1
RewriteRule ^articles/isfahan_articles([0-9]+).html$ maghalat.php?id=$1
After this major change Google.com dropped many of my page rankings and recently I have only 20% of normal site visitors.
My questions are:
Does this changes caused this drop of ranking, because in fact now I have 2 links for every content .
I fetch subject text by PHP and generate XXX_[article subject].html, how can I write a rewrite syntax that redirect isfahan_articlesXXX.html to XXX_[article subject].html file when XXX is article's ID in .htaccess file?
You should have 301 redirected your old links to your new links, after creating your new links.
If you did not or are duplicating links to the same content, this would indeed hurt your rankings.
UPDATE
Looks like you already have the RewriteRules in place that you need. Just add the following flags (note the 301 redirect for the old links) and over time it should (over time) rebuild your page rank.
RewriteRule ^([0-9]+)_([^/\.]+).html$ maghalat.php?id=$1 [L]
RewriteRule ^articles/isfahan_articles([0-9]+).html$ maghalat.php?id=$1 [R=301,L]

Multilanguage site, redirect based on cookie or accept-language (RewriteRule)

A few weeks ago i asked a question about a multilanguage site (see Multilanguage site, subdirectories as language (RewriteRule)).
That works perfect (thanks for that)
But since I want to make my website nicer and I started adding more languages a new problem showed up.
I want to see if I can redirect users directly to the site in their language, if this language does not exists or a cookie is set I want to redirect users to their last language or my default language.
For now I created this part in my .htaccess
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/?$ en/ [L,R=301]
This causes that all calls to www.xxx.com/ are redirected to www.xxx.com/en/
Now the second part, in my php code I store a cookie this cookie is set for www.xxx.com/
I tried the following:
RewriteCond %{HTTP_COOKIE} language=([^;]+) [NC]
RewriteRule ^/?$ %1/ [L,R=302]
But this gives me an infinite loop.
I also tried this, so if my language is nl redirect to www.xxx.com/nl/ but this also results in an infinite loop. Also this code should only be executed in some cases
RewriteCond %{HTTP:Accept-Language} (nl) [NC]
RewriteRule ^/?$ nl/ [L,R=302]
Can someone help me and is this possible in an htaccess or should I create some logic in PHP?
1 Check if cookie is set with a language ( I guess this should be the above stated code)
2a If a cookie is set redirect the user to that language
2b If no cookie is set check the http-accept language
2b1 If this language exists redirect to that language
2b2 If that language does not exists redirect to the default
Thanks in advance
Accept-Language is a list of weighted language identifiers. Just the occurrence of a certain language identifier does not mean that it’s the most preferred language. There might be other languages that are more preferred (higher q value) or even not accepted at all (i.e. q=0).
So instead of just looking for a certain language identifier, parse the list of accepted languages and find the best match of accepted languages and available languages while considering the preference order. And since you seem to be using PHP, do that with PHP instead of mod_rewrite.

Resources