We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 35756
    • 166 Posts
    Hi there!

    I ran into another problem by trying to redirect "old" paths from a previous install in the .htaccess-file which is placed in the root-directory.

    OLD PROJECT - working

    In another project (which is not using Babel) it works well by using this:

    Redirect 301 /old-folder http://www.website.de/index.php?id=38


    This code redirects me correctly to www.website.de/new-folder/

    Strangewise (or not?) I had to use it the way I posted it above with the index.php?id=38 to point to the correct page. When I tried to do it like this:

    Redirect 301 /old-folder http://www.website.de/new-folder/


    it resulted in this: http://www.website.de/newfolder/?q=old-folder/

    So I had to choose the way by using the resource-id.



    NEW PROJECT - not working

    On this new project - this time with Babel - I tried it the same way:

    Redirect 301 /en/site.html http://www.website.de/index.php?id=115


    But this results in the following: http://www.website.de/index.php?id=115


    EDIT: I also tried it using like this:

    Redirect 301 /en/site.html http://www.website.de/en/index.php?id=115


    which also didn't work and redirected to this: http://www.website.de/en/index.php?id=115

    EDIT2: Added some redirects to the main-context, which seem to be working...so the issues is related to the second context...



    I also tried to use the Redirector-package as well as the StercSEO Tab-Package, which seem not to be working correctly as well. Idk if it's just me, but redirecting in MODX always has been a huge struggle for me...

    Any help will be highly appreciated!

    This question has been answered by rainbowtiger. See the first response.

    [ed. note: profilneurotiker last edited this post 8 years, 10 months ago.]
      • 16899
      • 284 Posts
      Hi

      Did you try to activate friendly Url´s? I never tried Babel without friendly urls... but i believe that this could be the problem!!!

      By the way, check your context Values and let me know
        • 35756
        • 166 Posts
        Hello legues!

        And thank you for your reply!

        Friendly-URL's is activated in this project.

        And what do you mean by "check your context values"? The project is running well with friendly urls and two contexts, one for german (context name - "web") and one for english (context name - "english"). Or do you need some context-setting-values for further info?

        • can you post your entire htaccess's content?
            Rico
            Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
            MODx is great, but knowing how to use it well makes it perfect!

            www.virtudraft.com

            Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

            Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

            Maintainter/contributor of Babel

            Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
          • If the web server doesn't find the requested file or folder, it will load index.php with the q= value being the file or directory it didn't find. Everything after the domain.com/ bit goes into the q= value. When you specify index.php?id=115, the server has no problem, it finds the index.php right away.

            What you'll need is to have a plugin OnPageNotFound to replace that htaccess rewrite, switching the q=old-folder to q=new-folder. Then MODX can go ahead and deal with it, either silently or by using a sendRedirect() to the proper URL with the desired HTTP response code.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 35756
              • 166 Posts
              Hey there!

              Thanks for the replies!

              Here's my .htacess-file:


              # 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.
              #
              # 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]
              
              RewriteEngine On
              RewriteBase /
              
              RewriteCond %{HTTP_HOST} .
              RewriteCond %{HTTP_HOST} !^www\.website\.de [NC]
              RewriteRule (.*) http://www.website.de/$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/$1 [R=301,L]
              
              
              RewriteRule joomla http://www.website.de? [L,R=301]
              RewriteRule ck-editor http://www.website.de? [L,R=301]
              RewriteRule fckeditor http://www.website.de? [L,R=301]
              RewriteRule fck http://www.website.de? [L,R=301]
              RewriteRule Fck http://www.website.de? [L,R=301]
              RewriteRule FCK http://www.website.de? [L,R=301]
              ...
              
              # redirect all requests to /de/favicon.ico and /nl/favicon.ico
              # to /favicon.ico
              RewriteCond %{REQUEST_FILENAME} !-d
              RewriteCond %{REQUEST_FILENAME} !-f
              RewriteRule ^(de|en)/favicon.ico$ favicon.ico [L,QSA]
              
              RewriteCond %{REQUEST_FILENAME} !-d
              RewriteCond %{REQUEST_FILENAME} !-f
              RewriteRule ^(de|en)/layout.css$ layout.css [L,QSA]
                 
              # redirect all requests to /de/assets* and /nl/assets* to /assets*
              RewriteCond %{REQUEST_FILENAME} !-d
              RewriteCond %{REQUEST_FILENAME} !-f
              RewriteRule ^(de|en)/assets(.*)$ assets$2 [L,QSA]
              
              # redirect all requests to /de/assets* and /nl/assets* to /assets*
              RewriteCond %{REQUEST_FILENAME} !-d
              RewriteCond %{REQUEST_FILENAME} !-f
              RewriteRule ^(de|en)/pdf(.*)$ pdf$2 [L,QSA]
              
              # redirect all requests to /de/assets* and /nl/assets* to /assets*
              RewriteCond %{REQUEST_FILENAME} !-d
              RewriteCond %{REQUEST_FILENAME} !-f
              RewriteRule ^(de|en)/pressemitteilungen(.*)$ pressemitteilungen$2 [L,QSA]
              
              # redirect all requests to /de/assets* and /nl/assets* to /assets*
              RewriteCond %{REQUEST_FILENAME} !-d
              RewriteCond %{REQUEST_FILENAME} !-f
              RewriteRule ^(de|en)/images(.*)$ images$2 [L,QSA]
               
              # redirect all other requests to /de/* and /nl/*
              # to index.php and set the cultureKey parameter
              RewriteCond %{REQUEST_FILENAME} !-f
              RewriteCond %{REQUEST_FILENAME} !-d
              RewriteRule ^(de|en)?/?(.*)$ index.php?cultureKey=$1&q=$2 [L,QSA]
              
              Redirect 301 /stiftung/site.html http://www.website.de/index.php?id=8
              Redirect 301 /service.html http://www.website.de/index.php?id=18 
              
              Redirect 301 /en/service.html http://www.website.de/index.php?id=193
              
              # 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 by 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


              As you can see in line 71 I tried to redirect the url www.website.de/en/service.html to index.php?id=193 by using this:

              Redirect 301 /en/service.html http://www.website.de/index.php?id=193


              But this doesn't work; this leads to:

              http://www.website.de/index.php?id=193


              The resource with the ID of 193 is in a second context called "english".


              And I can't follow Susan's reply so far?! [ed. note: profilneurotiker last edited this post 8 years, 9 months ago.]
              • If you just use the regular rewrites, the web server will rewrite the request to
                http://www.website.de/index.php?q=en/service.html


                It is now up to your plugin "OnPageNotFound" to break up the value and use the "en" bit to switch the context to "en" from "web", then MODX can continue on, find and process the resource with the alias of "service".

                I use a plugin that doesn't require any modification to the basic .htaccess file. https://gist.github.com/gadamiak/3812853 (see the comments for a couple of fixes to handle the default context) Actually I believe that Bruno used this plugin for his MIGXMultilang package.
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                • I'm also fiddling with a modification to the main index.php file, where it initializes the 'web' context, to do pretty much the same thing with no need for even a plugin. It takes that first bit, checks it against the list of contexts, and if there is such a context, initializes that context. If not, it goes ahead and intializes the 'web' context.
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 35756
                    • 166 Posts
                    Hello again Susan!

                    And thanks again for your help on this!

                    So I tried to follow the instructions from the github-link you posted.

                    First I created a new plugin and made the changes you mentined below on the site. So my complete plugin-code looks like this:

                    <?php
                    /* LangRouter
                     * ==========
                     *
                     * This plugin is meant to be used with Babel extra for MODX Revolution. It 
                     * takes care of switching contexts, which hold translations, depending on URL 
                     * requested by client. LangRouter works with so called subfolder based setup, 
                     * in which many languages are served under a single domain but are 
                     * differentiated by a virtual subfolder indicating the language, eg. 
                     * mydomain.com/pl/.
                     *
                     * The routing work as follows:
                     * - if URI contains cultureKey, which is defined in Babel configuration, then 
                     *   the matching context is served
                     * - if URI doesn't contain cultureKey (or one not defined in Babel 
                     *   configuration) AND at least one of the client's accepted languages is 
                     *   defined in Babel configuration, then the matching context is served
                     * - otherwise the default context is served
                     *
                     * LangRouter works out-of-the-box and doesn't require any changes to URL 
                     * rewrite rules in the webserver configuration. All routing is handled 
                     * internally by MODX. This greatly simplifies the setup and provides 
                     * portability. LangRouter was tested with Apache and Lighttpd.
                     *
                     * Setup:
                     * 1. Prepare your contexts as you normally would for Babel.
                     * 2. For each context set `base_url` to `/`.
                     * 3. For each context set `site_url` to 
                     *    `{server_protocol}://{http_host}{base_url}{cultureKey}/`
                     * 4. Add new system setting `babel.contextDefault` and set it to the default 
                     *    context, which should be served when no language is specified in 
                     *    request, eg. `pl`.
                     * 5. Include static files from the assets folder with 
                     *    `[[++assets_url]]path/to/static_file`.
                     * 6. In template header use `<base href="[[++site_url]]" />`.
                     * 7. Use default URL generation scheme in MODX (ie. relative).
                     *
                     * This code is shared AS IS. Use at your own risk.
                     */
                    if($modx->context->get('key') != "mgr") {
                      /*
                       * Debugs request handling
                       */
                      function logRequest($message = 'Request')
                      {
                        global $modx;
                        $modx->log(modX::LOG_LEVEL_ERROR, $message . ':'
                          . "\n REQUEST_URI:   " . $_SERVER['REQUEST_URI']
                          . "\n REDIRECT_URI:  " . $_SERVER['REDIRECT_URI']
                          . "\n QUERY_STRING:  " . $_SERVER['QUERY_STRING']
                          . "\n q:             " . $_REQUEST['q']
                          . "\n Context:       " . $modx->context->get('key')
                          . "\n Site start:    " . $modx->context->getOption('site_start')
                        );
                      }
                      /*
                       * Dumps variables to MODX log
                       */
                      function dump($var) {
                        ob_start();
                        var_dump($var);
                        return ob_get_clean();
                      }
                      /*
                       * Detects client language preferences and returns associative array sorted 
                       * by importance (q factor)
                       */
                      function clientLangDetect()
                      {
                        $langs = array();
                        if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
                          # break up string into pieces (languages and q factors)
                          preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
                          if (count($lang_parse[1])) {
                            # create a list like "en" => 0.8
                            $langs = array_combine($lang_parse[1], $lang_parse[4]);
                            # set default to 1 for any without q factor
                            foreach ($langs as $lang => $val) {
                              if ($val === '') $langs[$lang] = 1;
                            }
                            # sort list based on value
                            arsort($langs, SORT_NUMERIC);
                            return $langs;
                          }
                        }
                      }
                      #logRequest('Unhandled request');
                      # Get contexts and their cultureKeys
                      $babelContexts = explode(',', $modx->getOption('babel.contextKeys'));
                      $languages = array();
                      foreach ($babelContexts as $context) {
                        $ctx = $modx->getContext($context);
                        $languages[$ctx->config['cultureKey']] = trim($context);
                      }
                      #$modx->log(modX::LOG_LEVEL_ERROR, dump($languages));
                      # Determine language from request
                      $reqCultureKeyIdx = strpos($_REQUEST['q'], '/');
                      $reqCultureKey = substr($_REQUEST['q'], 0, $reqCultureKeyIdx);
                      # Serve the proper context and language
                      if(array_key_exists(
                        strtolower($reqCultureKey), array_change_key_case($languages))) {
                          $modx->switchContext($reqCultureKey);
                          # Remove cultureKey from request
                          $_REQUEST['q'] = substr($_REQUEST['q'], $reqCultureKeyIdx+1);
                          #logRequest('Culture key found in URI');
                        } else {
                          $clientCultureKey = array_flip(
                            array_intersect_key(clientLangDetect(), $languages));
                          if($clientCultureKey) {
                            $contextDefault = current($clientCultureKey);
                          } else {
                            $contextDefault = trim($modx->getOption('babel.contextDefault'));
                          }
                          if($reqCultureKey == $defaultLang) $reqCultureKey = 'web';
                          #$modx->log(modX::LOG_LEVEL_ERROR, dump($contextDefault));
                          if($reqCultureKey == $modx->getOption('cultureKey')) $reqCultureKey = $modx->getOption('babel.contextDefault');
                          $modx->switchContext($contextDefault);
                          #logRequest('Culture key not found in URI');
                          $modx->sendRedirect($modx->context->getOption('site_url'));
                        }
                      # Serve site_start when no resource is requested
                      if(empty($_REQUEST['q'])) {
                        #$modx->log(modX::LOG_LEVEL_ERROR, 'Query is empty');
                        $modx->sendForward($modx->context->getOption('site_start'));
                      }
                    }
                    if($contextDefault == $modx->getOption('cultureKey')) $contextDefault = $modx->getOption('babel.contextDefault');


                    The I followd the instructions mentioned in the "descrition"-part at the top...

                    1. Context's already have been prepared
                    2. each context's base_url set to /
                    3. each context's site_url set to {server_protocol}://{http_host}{base_url}{cultureKey}/
                    4. created new system-setting "babel.contextDefault" and set it to "web" (because this is the default context)
                    5. this point i didn't understand?
                    6. <base href="[[++site_url]]" /> was already inside all templates-headers
                    7. this point i didn't understand too?

                    Then I cleared the cache and made the url-actualisation.

                    But the result was getting an empty/blank site without any code in the source or any information given by the console...so I'm stuck again sad


                    About your second post:

                    do I get you right to edit the index.php with some php-code to grab the url, check if there's an "/en/" implemented, if so initialize "en"-context, otherwise initialize "web"-context?
                      • 35756
                      • 166 Posts
                      And another question that came to mind:

                      the way I'am redirecting the standard-context url's is working...but is this the correct way to pass the pagerank of the old sites over to the new redirected sites?

                      Redirect 301 /old-folder http://www.website.de/new-folder/


                      I'm fearing a bit that the pagerank might get lost?