ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

htaccess strict enforcement of language segment

July 17, 2012 1:01pm

Subscribe [1]
  • #1 / Jul 17, 2012 1:01pm

    John St-Amand

    865 posts

    Hey there follow EE-ers.  I have an htaccess question I hope you can help me with since it’s not my strongest area.

    I have a site that’s just about ready to go live that is a multilingual MSM deployment.  My question really only pertains to one site within the MSM structure, though i suspect the solution will be utilized on the others as well.

    We’re using the multi-language alternative approach for EE2 as described in the Wiki. As outlined, we have a directory for each language and each containing a copy of index.php within which a user_language is defined, matching the language segment of en, fr, etc.

    The trick is that in the root of the domain, in which one would find the original index.php, even if we define a language segment, that will add itself to all paths in EE - but if a user were to go into the address bar and remove said language segment, EE would still display the resulting page.  Even with strict URL enforcement in EE, I’m not sue this is ideal.  We would really want to force the server to apply a language segment in the event the language segment is removed from the URL - thereby preventing a whole host of 404 issues, not to mention Google potentially indexing the same content at the URLs with and without the language segment.

    So my question is… can htaccess be used to automatically apply a language segment to the URL if the language segment is not detected?

  • #2 / Jul 17, 2012 1:05pm

    To make sure I understand, you need something that does the following:

    example.com 301 redirects to: example.com/en
    example.com/foo 301 redirect to: example.com/en/foo

    ... right?

  • #3 / Jul 17, 2012 1:13pm

    John St-Amand

    865 posts

    No, mostly just the first one I think.  For example:

    domain.com/ leads to home page - but should probably automatically go to domain.com/en

    But the biggest issue is:

    domain.com/en/about and domain.com/about both render the same thing since about is a template group index.

    And so I would want domain.com/about to automatically resolve to domain.com/en/about to prevent a user from removing the language segment and still receiving content.

    That make sense?

  • #4 / Jul 17, 2012 1:50pm

    Gotcha; I think we’re both trying to say the same thing in both posts there…

    In any event, this worked in my development environment; it should work for you too. You’ll want this rule to appear before any of your other EE htaccess rules.

    # ---------------------------------------------------------------------
    # Enable Rewrite Engine
    # ---------------------------------------------------------------------
    
    RewriteEngine on
    RewriteBase /
    
    # ---------------------------------------------------------------------
    # example.com/foo -> example.com/en/foo
    #
    # For any request
    # ... that is an Apache GET request ...
    # ... and isn't a file ...
    # ... and isn't isn't a directory ...
    # ... and isn't within a one of the specified language directories ...
    # Then ...
    # ... redirect to address with /en/
    # ---------------------------------------------------------------------
    
    RewriteCond %{THE_REQUEST} ^GET [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(en|es|de)/(.*)$ [NC]
    RewriteRule ^(.*)$ en/$1 [R=301,L]
    
    # Other redirects can go below this line…
  • #5 / Jul 17, 2012 1:53pm

    John St-Amand

    865 posts

    Awesome Nick. THanks.  We’ll give it a try and I’ll let you know how we make out - the only other htaccess rules we have in place are the usual remove index.php rewrites, so hopefully this will do the trick.

  • #6 / Jul 17, 2012 3:59pm

    John St-Amand

    865 posts

    Hi Nick - ok, something’s a little off - could be entirely due to the other items in the htaccess file.  Here’s what I had previously:

    <IfModule mod_rewrite.c>
    RewriteEngine On
           RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
           RewriteCond %{REQUEST_FILENAME} !-f
           RewriteCond %{REQUEST_FILENAME} !-d
           RewriteRule ^(.*)$ /en/index.php/$1 [L]
    </IfModule>

    And after incorporating your suggested changes:

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteCond %{THE_REQUEST} ^GET [NC]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond $1 !^(en|es|de|fr)/(.*)$ [NC]
        RewriteRule ^(.*)$ en/$1 [R=301,L]
        RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ /en/index.php/$1 [L]
    </IfModule>

    Now if i visit a URL that previously was valid - so domain.com/en/about for example, I get:

    The page isn’t redirecting properly
       
    Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

    Meanwhile domain.com/about is rendering just fine - which is what I don’t want to have happen. Am I a little backwards?  Spot anything off in what i have in the new version above?

    I appreciate your patience, Nick. htaccess is a bit greek to me.

  • #7 / Jul 17, 2012 4:39pm

    Do you have multiple .htaccess file setup? Perhaps one in each language’s directory? Example:

    - example.com/.htacces
    - example.com/en/.htaccess
    - example.com/es/.htaccess

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases