2

we have a multilingual website and everything is based on the language segment in the uri. eg.

  • /en/home
  • /fr/home
  • /nl/home

Now I want my 404 page for a uri without a language segment to redirect to one with a language segment, preferably based on the browser language. That way my 404 page will hava a language attached.

eg. http://www.website.be/test should redirect to http://www.website.be/en/test (when EN is the browserlanguage)

At the moment this is my .htaccess file

            # Add www - only for production urls
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} !^website\.localhost [NC]
    RewriteCond %{HTTP_HOST} !^192\..+$ [NC]
    RewriteCond %{HTTP_HOST} !^dev\..+$ [NC]
    RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    # REMOVE TRAILING SLASH
    # Make sure it's not a directory root
    RewriteCond %{REQUEST_FILENAME} !-d
    # Catch any+all characters and throw a slash in as the last character
    RewriteCond %{REQUEST_URI} ^(.+)/$
    # Redirect to same URI sans trailing slash (and use 301 for SEO goodness)
    RewriteRule ^(.+)/$  /$1 [R=301,L]

    # Removes index.php
    RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php?$1 [L]

    # Redirect based on browser accept language FR
    RewriteCond %{HTTP:Accept-Language} ^fr [NC]
    RewriteCond %{QUERY_STRING} !^(ACT=.*)
    RewriteCond %{QUERY_STRING} !^(MOBILE_ACT=.*)
    RewriteRule ^$ /fr [L,R=301]

    # Redirect based on browser accept language EN
    RewriteCond %{HTTP:Accept-Language} ^en [NC]
    RewriteCond %{QUERY_STRING} !^(ACT=.*)
    RewriteCond %{QUERY_STRING} !^(MOBILE_ACT=.*)
    RewriteRule ^$ /en [L,R=301]

    #NO LANGUAGE FOUND - NL = DEFAULT
    RewriteCond %{QUERY_STRING} !^(ACT=.*)
    RewriteCond %{QUERY_STRING} !^(MOBILE_ACT=.*)
    RewriteRule ^$ /nl [L,R=301]
3
  • If you're relying on the browser setting to redirect language, are you prohibiting the user from specifying a language? What if my browser is set to EN but I prefer to read in FR? Commented Jan 31, 2013 at 14:35
  • there's no "choose your language" page, so the best we can do is to guess the language. If we guessed wrong, the user can still switch his language in the upper right corner of the site.
    – Vic
    Commented Jan 31, 2013 at 16:22
  • Once the user switches his language, how are you handling it then? How is your site tracking and reading the user's language selection, and wouldn't the redirects continually override that? Commented Jan 31, 2013 at 17:48

2 Answers 2

2

Rather than do it via htaccess why not use EE segment tags.

{if segment_1 != "en"}{if segment_1 != "fr"}{redirect="/en/"}{/if}{/if}

I've nested so that it's an early parsed simple conditional.

Otherwise you'll be getting into the realm of settings cookies and reading them in the htaccess.

2
  • should segment_1 not be placed between {} your approach didn't really work for me, had some htaccess conflicts and redirect loops... but that's maybe specific to my setup.
    – Vic
    Commented Feb 4, 2013 at 20:15
  • @Vic, Nothing's needed around segment_1 unless you want to force it to be a late parsed advanced conditional. I'd recommend renaming your htaccess file and putting index.php back in for testing. It sound like you've got a problem with your setup.
    – Paul
    Commented Feb 5, 2013 at 20:46
0

Used the PHP solution below in my 404 template based on Paul's suggestion, so no htaccess more:

<?php

    $segment_1 = $this->EE->TMPL->segment_vars["segment_1"];
    if($segment_1!="en" && $segment_1!="fr" && $segment_1!="nl" && $segment_1!="de")
    {
        redirect('/en/' . $segment_1, 'location', 301);
    }

    $this->EE->output->set_header("HTTP/1.0 404 Not Found");
?> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.