We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15472
    • 14 Posts
    I’m still attempting to learn all the ins and outs of .htaccess but thought someone might be able to help me. Been banging my head over this for the last few hours.

    My previous site used URLs like: http://sitename.com/index.php?option=variable

    My new MODx site uses standard MODx URLs that are rewritten successfully.

    Now, I’d like to permanently redirect the old URLs (which are causing 404 errors) to the appropriate pages within the new MODx installation.

    Basically, I want to check a URL to see if the variable is "option" and if it is a particular "option" I want to redirect to the appropriate URL within MODx.

    In PHP I’d write this as:
    If ($_GET("option")="variable1"){
    echo Meta Refresh code here;
    }else{
    Standard MODx rewrite conditions
    }
    


    Any way to do something similar using permanent redirects in .htaccess?

    *Hope that was clear.
      • 15472
      • 14 Posts
      ::bump::

      Anyone?
      • Lot’s of ways to do it probably. Best for this would be a RewriteRule for your old URL pattern... I tested this only a little and the actual rules will differ slightly from environment to environment (e.g. this is installed under /modx/ in my web server, thus the /modx/ references in the code below), so you may need to do some research on authoring RewriteRules in general or for your specific server, but it would be something like this to send the user to a page with the alias equal to a former ’option’ parameter:

        RewriteEngine On
        
        RewriteCond %{QUERY_STRING} ^option=([^&;]*)$
        RewriteRule ^index.php$ /modx/%1 [R,L]
        
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        
        # If your MODx installation is in a subdirectory, change the following line to match the physical
        # path to the "root" of the site as follows:
        # RewriteRule ^(.*)$ /path/to/subdirectory/index.php?q=$1 [L,QSA,NC]
        
        RewriteRule ^(.*)$ /modx/index.php?q=$1 [L,QSA,NC]
        


        Obviously, this is limited and still keeps the option= parameter on the url, but it is a start and you should be able to customize those rules from here with just as much ease as I could. tongue Unfortunately, trial and error is the best way to handle mod_rewrite rules, and lots of Googling...unless you’re already a guru wink
          • 263
          • 52 Posts
          Hi Kaust!

          Without more details and concrete examples, it seems difficult to offer you a specific solution. But I’m sure it’s there somewhere in the Apache docs, between mod_rewrite and mod_alias and all the different flavoured directives therein.
          But why not keep it simple: a customised 404 error page, informing of your site upgrade and extolling the virtues of MODx, maybe with a site-map and latest news posts or whatever.
          Something like this perhaps: http://www.456bereastreet.com/somefilenolongerhere

          Cheers
            • 15472
            • 14 Posts
            Thanks for the response Jason... Mostly worked but still running into an issue.

            This is my full htaccess:
            # Compression
            # and may also be optionally commented out.
            php_flag zlib.output_compression On
            php_value zlib.output_compression_level 5
            
            #Rewrite On and SymLinks
            RewriteEngine On
            Options +FollowSymlinks
            
            #Rewrite and redirect specific old URLs with 301 loaded for SEs
            RewriteCond %{QUERY_STRING} ^option=foobar
            RewriteRule ^index.php http://site.com/dir/specificpage.html [L,R=301]
            
            #Force http://site.com rather than www. for non-duplication in SEs
            RewriteCond %{HTTP_HOST} ^www\.site\.com [nc]
            RewriteRule (.*) http://site.com/$1 [R=301]
            
            #Check for existing directory or file
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            
            # Rewrite SE friendly URLs for MODx v3
            RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA,NC]


            This code actually works. It takes a specific parameter (option=specific) and then redirects (301) to the proper URL for the new page. This is only for specific requested <i>option</i>. Unfortunately, it rewrites the URL like: <b>http://site.com/dir/page.html?option=foobar</b>.

            Any suggestions on how to rid myself of the <b>option=foobar</b> at the end of the rewritten URL?

            Abbeyroad, I am forcing 301s for the search engines that index my site and for the numerous hits to specific pages from those engines. Forcing a 301 will instruct search engines (and end-users for that matter) that the page has moved and provide them with the new URL (so the SEs can update their databases and the end-user can update their bookmarks) rather than showing a 404 page and making them dig through a site map.
            • Not really sure how to solve that; I came up with those rules simply by googling and researching similar issues, but my mod_rewrite expertise is a bit limited. Best I can offer is make some changes to it and start trying it out; that’s how I got it working at all....I thought leaving off the QSA part of it would prevent the query string from being appended, but...no luck wink Check out http://forum.modrewrite.com/ for some great examples and probably mo’ better help with authoring mod_rewrite rules.
                • 15472
                • 14 Posts
                Jason, thanks for the link. Posted over there hoping to get some help from an htaccess guru.