user3318780
user3318780

Reputation: 1

htaccess language redirect with manual language selector

I use WPML to translate my Wordpress site, I don't want to use a Jquery Language redirect but an htaccess one.

http://example.com is the default EN version

http://example.com/de is the german one

The problem is when you're redirected to the /de/ you can't navigate manually to the EN version.

Here is my code

## Language Detection

#The 'Accept-Language' header starts with 'de'
#and the test is case-insensitive ([NC])
RewriteCond %{HTTP:Accept-Language} ^de [NC]

#If not already redirected
RewriteCond %{REQUEST_URI} !^/de/ [NC]    # ADDED

#Redirect user to /de/ address
#sending 301 (Moved Permanently) HTTP status code

RewriteRule ^$ /de/ [L,R=301]

#For every other language use English
RewriteRule ^$ - [L]    # MODIFIED

Upvotes: 0

Views: 1697

Answers (1)

user3934058
user3934058

Reputation:

I am not sure why you don't use the inbuilt Browser Language redirection WPML provides? https://wpml.org/documentation/getting-started-guide/language-setup/automatic-redirect-based-on-browser-language/

Using WPML you should not redirect via .htaccess, this is known to be a problematic approach.

I also found this on the forums of WPML:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

# Redirect. Eg. Browser in German, redirect to /de/
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:Accept-Language} (de) [NC]
RewriteRule .* http://your-site.com/de/$1 [L]

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp/index.php [L]
</IfModule>

# END WordPress

I found this here: https://wpml.org/forums/topic/multiple-languages-without-cookies/ and adopted the code to your German example.

The point is, you would need to create a directory for the standard language, other wise switching languages with the Selector will redirect you to the Homepage by your .htaccess rule

Hope that helps?

Upvotes: 1

Related Questions