dubbs
dubbs

Reputation: 1199

htaccess mod-rewrite language overrides

I am using some mod-rewrite code in my sites .htaccess file to redirect users with browser languages set to various languages to different versions of my site. This is working well - but... I need a way to be able to override these rewrite rules when a user wants to view the main English version instead of the version they would get automatically redirected to based on the language setting of their browser.

So for example, user A has their browser language set to Korean, they visit the example.com site - the rewrite rules reads they are set to Korean and moves them to example.co.kr - on example.co.kr I have a button which says - Show me the English version of this website (which links to example.com) - this then links back to example.com... but currently then redirects back to example.co.kr as the rewrite script refires...

How can I code the rewrite script to allow for an overwrite rule to force it to stay on the example.com site when I want it too...?

rewrite code I have is:

RewriteEngine On
RewriteBase /

#RewriteCond %{HTTP:Accept-Language} ^en [NC]
#RewriteRule ^$ https://example.com/ [L,R=301]

RewriteCond %{HTTP:Accept-Language} ^ko [NC]
RewriteRule ^$ https://example.co.kr/ [L,R=301]

RewriteCond %{HTTP:Accept-Language} ^pt [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]

RewriteCond %{HTTP:Accept-Language} ^pt-PT [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]

RewriteCond %{HTTP:Accept-Language} ^pt-BR [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(|ko|pt|pt-PT|pt-BR)/?$ https://example.com/ [QSA,NC,L]

Any ideas?

Cheers,

Upvotes: 1

Views: 187

Answers (1)

anubhava
anubhava

Reputation: 784958

You need to modify your rules to look for a special query parameter e.g. notKO like this:

RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} !notKO [NC]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ https://example.com/ [L,R=301]

RewriteCond %{QUERY_STRING} !notKO [NC]
RewriteCond %{HTTP:Accept-Language} ^ko [NC]
RewriteRule ^$ https://example.co.kr/ [L,R=301]

RewriteCond %{QUERY_STRING} !notKO [NC]
RewriteCond %{HTTP:Accept-Language} ^pt [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]

RewriteCond %{QUERY_STRING} !notKO [NC]
RewriteCond %{HTTP:Accept-Language} ^pt-PT [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]

RewriteCond %{QUERY_STRING} !notKO [NC]
RewriteCond %{HTTP:Accept-Language} ^pt-BR [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(|ko|pt|pt-PT|pt-BR)/?$ / [NC,L]

Now you need to start sending ?notKO query parameter on your show me abc version of this website button click.

Clear your browser before you test this change.

Upvotes: 1

Related Questions