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

    I am wondering if anyone knows the solution to this.

    I am trying to get htaccess redirects working seamlessly from www. to non www. and vice versa when using SEO friendly urls but I am a little stuck and presume it has something to do with the SEO friendly urls rewrite.

    When I try forwarding a non www domain to the www like so, I get http://www.domain.com/index.php?q=services instead of http://www.domain.com/services

    eg
    forwarding...
    http://domain.com/services
    to the www equivalent of...
    http://www.domain.com/services
    returns this instead...
    http://www.domain.com/index.php?q=services
    

    The same happens in reverse. When I try forwarding a www domain to the non www like so, I get http://domain.com/index.php?q=services instead of http://domain.com/services:

    eg
    forwarding...
    http://www.domain.com/services
    to the non www equivalent of...
    http://domain.com/services
    returns this instead...
    http://domain.com/index.php?q=services


    Any ideas on how to make the 2 rules work together?

    This is the relevant code in my htaccess file

    RewriteEngine on
    RewriteBase /
    
    #rewrite URLs to make Search Engine Friendly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
    
    #redirect non www to www
    RewriteCond %{HTTP_HOST} !^www\.DOMAIN\.com$
    RewriteRule ^(.*)$ http://www.DOMAIN.com/$1 [R=301]


    Thanks in advance. [ed. note: nickf08 last edited this post 13 years, 8 months ago.]
      Web design Adelaide
      http://gocreate.com.au
      • 3749
      • 24,544 Posts
      Is there some reason you're not using the MODX default file for this? It's the ht.access file in the MODX root directory. The usual method is to rename it to .htaccess and uncomment whichever of the WWW rewrite sections you want (but not both).

      The WWW section needs to be above the Friendly URLs section.
        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
        • 21417
        • 486 Posts
        Quote from: BobRay at Jan 24, 2013, 01:49 AM
        Is there some reason you're not using the MODX default file for this?

        Yes. User oversight and morphing over the years. Ha, that was easy for the non www to the www, thanks BobRay. That's great.

        RewriteEngine On
        RewriteBase /
        
        # Rewrite domain.com -> www.domain.com -- used with SEO Strict URLs plugin
        RewriteCond %{HTTP_HOST} .
        RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
        RewriteRule (.*) http://www.example.com/$1 [R=301,L]
        
        # For Friendly URLs
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
        


        In the 1.0.7 ht.access sample file there is no example for www to non www and when I try the above with the www's removed, I get a 500 server error:

        RewriteCond %{HTTP_HOST} .
        RewriteCond %{HTTP_HOST} !^example\.com [NC]
        RewriteRule (.*) http://example.com/$1 [R=301,L]


        Any ideas on the www to non www?
          Web design Adelaide
          http://gocreate.com.au
          • 3749
          • 24,544 Posts
          Did you replace 'example.com' with your actual domain name? Here's the section from the Revo ht.access file:


          # 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]
            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
            • 21417
            • 486 Posts
            Quote from: BobRay at Jan 24, 2013, 05:00 PM
            Did you replace 'example.com' with your actual domain name?

            I sure did, thanks BobRay. The original code I put in was correct but for some reason when I tested the redirect from non www to www, and then straight away changed the redirect of www to non www in the htaccess file, the 500 error was thrown. Either the server or my browser (FF 15) was temporarily holding onto the past (hey I'm doing it with Evo).

            Many thanks again BobRay, I really appreciate all your help.

            To confirm, if anyone else needs help with this, the code that works for me to redirect www to non www is:

            RewriteEngine On
            RewriteBase /
             
            # Rewrite www.domain.com -> domain.com -- used with SEO Strict URLs plugin
            RewriteCond %{HTTP_HOST} .
            RewriteCond %{HTTP_HOST} !^DOMAIN\.com [NC]
            RewriteRule (.*) http://DOMAIN.com/$1 [R=301,L]
             
            # For Friendly URLs
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]


            Cheers BobRay.
              Web design Adelaide
              http://gocreate.com.au