We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34108
    • 66 Posts
    I've come across something weird when trying to set up .htaccess 301 redirects for a client's site we just launched.

    redirect 301 /contact-us.php http://www.3pconsulting.com.au/contact

    should redirect any instances of the old link http://www.3pconsulting.com.au/contact-us.php to the new link - http://www.3pconsulting.com.au/contact

    But instead it goes to http://www.3pconsulting.com.au/contact?q=contact-us.php

    This seems to be related to the friendly URL's part of the .htaccess file marked in bold below:

    # The Friendly URLs part
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]


    If I change that last line to remove the ?q=$1, e.g.

    RewriteRule ^(.*)$ index.php [L,QSA]


    Suddenly the 301 redirects work fine but Wayfinder breaks.

    I'm thinking this may be a bug but wanted to double-check here first?

    Many thanks smiley

    Harmony
    [ed. note: hsteel last edited this post 12 years, 5 months ago.]
      • 3749
      • 24,544 Posts
      I'm not sure this will help and I definitely don't claim to be an .htaccess guru, but I recently had a similar problem (two problems, actually) with the .htaccess rewrite processing.

      One turned out to be because the ###.shtml files (403.shtml, etc.) were missing. The host had put them in the public_html directory, but MODX was in a subdirectory. When they weren't found, Apache would generate a second request that didn't trigger the original rewrite rule but also didn't seem to change the $_SERVER variables.

      I also had the trouble you did with a straight redirect rule (though it may have been related to the problem above). That didn't work, but something like this did:

      RewriteCond %{QUERY_STRING}  ^$
      RewriteRule ^contact-us\.php$ contact? [R=301,NE,L]
      


      You definitely don't want to modify the FURLs code.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 34108
        • 66 Posts
        Thanks Bob smiley! Your solution worked a treat. I'll be using that method for my 301 redirects from now on.

        I really appreciate your help! Thanks again.

        Harmony
          • 37763
          • 12 Posts
          hi hsteel,, i have same problem here... could you more explain how to fix it...
            • 37763
            • 12 Posts
            just found it, like this:
            RewriteRule ^old/old-page.html http://www.domain.com/new-page.html [R=301,L]

            what do you all think about this?
              • 34108
              • 66 Posts
              Hi imamwbs, this is an example of the code I pasted at the top of my .htaccess document for the 301 redirect. This redirected from www.example.com\staff.htm to www.example.com\profiles

              RewriteEngine On
              RewriteBase /

              # 301 Redirects
              RewriteCond %{QUERY_STRING} ^$
              RewriteRule ^staff\.htm$ profiles? [R=301,NE,L]
                • 3749
                • 24,544 Posts
                Basically, the RewriteCond line is the same for all redirects. For the RewriteRule, you put RewriteRule followed by a space, followed by the bad URL followed by a space, followed by the good URL, followed by a question mark (no space before the question mark) followed by a space and then the [R=301,NE,L] part, which is always the same.

                Note that in the part right after RewriteRule (the bad URL) has to have a ^ and the beginning and a $ at the end and dots and slashes in the URL must be escaped with a preceding backslash:

                So

                assets/tutorial1.htm


                would be written as

                ^assets\/tutorial1\.htm$


                The URL you're redirecting to can be written as is.

                If you have a bunch or them, you only need one RewriteCond line at the top. Here is an example from my site (developed as a result of using the LogPageNotFound plugin):

                RewriteCond %{QUERY_STRING}  ^$
                RewriteRule ^MODX-revolution\.html$ /modx-revolution.html? [R=301,NE,L]
                RewriteRule ^Modx\.html$ /modx.html? [R=301,NE,L]
                RewriteRule ^MODx\.html$ /modx.html? [R=301,NE,L]
                RewriteRule ^modx-faq\.html$ /modx-faqs.html? [R=301,NE,L]
                RewriteRule ^modx-newbie-\.html$ /modx-newbie-faq.html? [R=301,NE,L]
                RewriteRule ^snippet-tutorials\.html$ /package-tutorials.html? [R=301,NE,L]
                RewriteRule ^spform-tutorial\.html$ /spform-snippet-tutorial.html? [R=301,NE,L]
                RewriteRule ^modx-snippet-parameters\.html$ /modx-snippet-properties.html? [R=301,NE,L]
                RewriteRule ^emailresource-tutorial\.html$ /emailresource-plugin-tutorial.html? [R=301,NE,L]
                RewriteRule ^getdynadescription-tutorial\.html$ /getdynadescription-snippet-tutorial.html? [R=301,NE,L]
                RewriteRule ^CopyingFiles\.html$ /copyingfiles.html? [R=301,NE,L]
                RewriteRule ^modxcms.com[/|\/]forums$ http://forums.modx.com? [R=301,NE,L]
                
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 37763
                  • 12 Posts
                  Thanks all, my redirect page already working now...