We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36571
    • 145 Posts
    Hey all,

    I want to set up a redirect in my htaccess file to direct the visitors to the correct language site based on their browser language. To do this I tried adding the following to the default htaccess file that comes with 0.9.6.3:

    RewriteEngine on
    RewriteCond %{HTTP:Accept-Language} (de) [NC]
    RewriteRule .* www.mysite.com/deutsch [L]


    However, this seems to be conflicting with something that already exists in the file and it does not redirect properly.

    Any idea on how to fix this.

    EDIT: Should probably mention that Friendly URLs are on and otherwise working correctly.
    Thanks.
    • This is definitely an .htaccess question... where did you put those lines in your .htaccess file? Where they are may affect how the site behaves.

      Could you paste the important parts of the file for us? I mean, you can leave out the comments, just paste the active lines, or just paste the whole thing. Then we can have a better idea of what’s going on.
        • 36571
        • 145 Posts
        I posted the lines at the end of the standard .htaccess file. In other words, my complete htaccess file now reads

        # MODx supports Friendly URLs via this .htaccess file. You must serve web
        # pages via Apache with mod_rewrite to use this functionality, and you must
        # change the file name from ht.access to .htaccess.
        #
        # Make sure RewriteBase points to the directory where you installed MODx.
        # E.g., "/modx" if your installation is in a "modx" subdirectory. If you have
        # problems with your .htaccess working at all, try un-commenting the first 
        # line above the "RewriteEngine On" directive.
        #
        # You may choose to make your URLs non-case-sensitive by adding a NC directive
        # to your rule: RewriteRule ^(.*)$ index.php?q=$1 [L,QSA,NC]
        
        #Options +FollowSymlinks
        RewriteEngine On
        RewriteBase /
        
        
        # Fix Apache internal dummy connections from breaking [(site_url)] cache
        RewriteCond %{HTTP_USER_AGENT} ^.*internal\ dummy\ connection.*$ [NC]
        RewriteRule .* - [F,L]
        
        
        # Rewrite www.domain.com -> domain.com -- used with SEO Strict URLs plugin
        #RewriteCond %{HTTP_HOST} .
        #RewriteCond %{HTTP_HOST} !^example-domain-please-change\.com [NC]
        #RewriteRule (.*) http://example-domain-please-change.com/$1 [R=301,L]
        #
        # or for the opposite domain.com -> www.domain.com use the following
        # >>> DO NOT USE BOTH THE ABOVE AND BELOW <<<
        #
        #RewriteCond %{HTTP_HOST} .
        #RewriteCond %{HTTP_HOST} !^www\.example-domain-please-change\.com [NC]
        #RewriteRule (.*) http://www.example-domain-please-change.com/$1 [R=301,L]
        
        
        
        # Rewrite secure requests properly to prevent SSL cert warnings, e.g. prevent 
        # https://www.domain.com when your cert only allows https://secure.domain.com
        #RewriteCond %{SERVER_PORT} !^443
        #RewriteRule (.*) https://example-domain-please-change.com.com/$1 [R=301,L]
        
        
        
        # The Friendly URLs part
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
        
        
        
        # Make sure .htc files are served with the proper MIME type, which is critical # for XP SP2. Un-comment if your host allows htaccess MIME type overrides.
        
        #AddType text/x-component .htc
        
        
        
        # If your server is not already configured as such, the following directive
        # should be uncommented in order to set PHP's register_globals option to OFF.
        # This closes a major security hole that is abused by most XSS (cross-site
        # scripting) attacks. For more information: http://php.net/register_globals
        #
        # To verify that this option has been set to OFF, open the Manager and choose
        # Reports -> System Info and then click the phpinfo() link. Do a Find on Page
        # for "register_globals". The Local Value should be OFF. If the Master Value
        # is OFF then you do not need this directive here.
        #
        # IF REGISTER_GLOBALS DIRECTIVE CAUSES 500 INTERNAL SERVER ERRORS :
        #
        # Your server does not allow PHP directives to be set via .htaccess. In that
        # case you must make this change in your php.ini file instead. If you are
        # using a commercial web host, contact the administrators for assistance in
        # doing this. Not all servers allow local php.ini files, and they should
        # include all PHP configurations (not just this one), or you will effectively
        # reset everything to PHP defaults. Consult www.php.net for more detailed
        # information about setting PHP directives.
        
        #php_flag register_globals Off
        
        
        
        # For servers that support output compression, you should pick up a bit of
        # speed but un-commenting the following lines.
        
        #php_flag zlib.output_compression On
        #php_value zlib.output_compression_level 5
        
        
        
        # The following directives stop screen flicker in IE on CSS rollovers. If
        # needed, un-comment the following rules. When they're in place, you may have
        # to do a force-refresh in order to see changes in your designs.
        
        #ExpiresActive On
        #ExpiresByType image/gif A2592000
        #ExpiresByType image/jpeg A2592000
        #ExpiresByType image/png A2592000
        #BrowserMatch "MSIE" brokenvary=1
        #BrowserMatch "Mozilla/4.[0-9]{2}" brokenvary=1
        #BrowserMatch "Opera" !brokenvary
        #SetEnvIf brokenvary 1 force-no-vary
        
        RewriteEngine on
        RewriteCond %{HTTP:Accept-Language} (es) [NC]
        RewriteRule .* http://www.mysite.com/espanol [R,L]
          • 25483
          • 741 Posts
          You can also redirect with the use of a snippet:

          <?php
          $lang = strtok($_SERVER['HTTP_ACCEPT_LANGUAGE'],",");
              while ($lang)
              {
                   //check if the language is dutch
                   if (strstr($lang,"nl"))
                   {
                        header ("location: ".$modx->makeUrl(2, '', '', 'full'));
                        exit;
                    }
                    //check if the language is english
                    if (strstr($lang,"en"))
                    {
                        header ("location: ".$modx->makeUrl(3, '', '', 'full'));
                        exit;
                    }
                  // etc..
                           
                   $lang = strtok(",");
               }
               
               // no defined language found, go to the english pages
              header ("location: ".$modx->makeUrl(3, '', '', 'full'));
              exit;
          ?>
          


          Create that as snippet and call it from your start page from the modx site. The number in the makeUrl function is the id of the page where you want to go to.

          Is use this script on multiple sites, it works great smiley
            with regards,

            Ronald Lokers
            'Front-end developer' @ h2o Media

            • 36571
            • 145 Posts
            Seems to be working great.

            Thanks a lot!
              • 36571
              • 145 Posts
              Hey.

              Just noticed a little quirk with this script. If the user language is set to something not specified in the snippet, then I get an error. Is it possible to direct these "exotic" users to the english version if their language is not specified in the snippet?

              Thanks.
                • 25483
                • 741 Posts
                What error do you get? The snippet should redirect the users with a unknown language to the english page like I did in my example.
                  with regards,

                  Ronald Lokers
                  'Front-end developer' @ h2o Media

                  • 36571
                  • 145 Posts
                  Very odd. When I tried it last week, I got the error - Parser timing out, I think.

                  And now I just get a blank screen.

                  This happens when I set my FF language option to Catalan [ca] (which is not likely to encompass a huge proportion of my visitors).

                  I think this may be a FF quirk, as in my visitor logs I have visitors from France and Italy automatically being redirected to the english page, so it is probably working fine.
                    • 2940
                    • 17 Posts
                    In the media_guy snippet add this line just after the $lang:

                    header ('HTTP/1.1 301 Moved Permanently');


                    if not added the http header sent is 302(found) and people say that search engines like the 301’s more.
                      • 25483
                      • 741 Posts
                      Quote from: BDA at Mar 11, 2011, 10:33 AM

                      In the media_guy snippet add this line just after the $lang:

                      header ('HTTP/1.1 301 Moved Permanently');


                      if not added the http header sent is 302(found) and people say that search engines like the 301’s more.

                      I’m not sure what search engines will do if they see the 301 header, because it tells them that for every different language the main url is changed. And that’s not what’s happened because every language has it’s own page.
                        with regards,

                        Ronald Lokers
                        'Front-end developer' @ h2o Media