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

Answered www Redirect

    • 40088
    • 708 Posts
    I recently moved to A2 Hosting and it seems my site no longer redirects from http://www.example.com to http://example.com despite the default MODX setting:

    # Rewrite www.domain.com -> domain.com -- used with SEO Strict URLs plugin
    RewriteCond %{HTTP_HOST} .
    RewriteCond %{HTTP_HOST} !^example\.com [NC]
    RewriteRule (.*) http://example.com/$1 [R=301,L]


    A2 tells me I need to use their code instead:

    # Remove www from any URLs that have them:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.
    RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]


    My question: Is there any reason why I shouldn't mess with the default MODX .htaccess code?

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

      Todd
    • discuss.answer
      • 3749
      • 24,544 Posts
      I really don't see why it would make any difference, although their rule seems a little less reliable to me, since I don't think it will capture URLs that start with WWW in uppercase.

      The MODX one rewrites anything that doesn't start with example.com, theirs rewrites only URLs that start with 'www.'.

      You might try this:

      # Rewrite www.domain.com -> domain.com -- used with SEO Strict URLs plugin
      RewriteCond %{HTTP_HOST} .
      RewriteCond %{HTTP_HOST} !^example\.com [NC]
      RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
      



      All that said, there's nothing critical about the MODX ht.access file except the part that does the friendly URL translation and presumably you're leaving that alone.
        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
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        No reason at all. The .htaccess file actually has nothing to do with MODX, it's instructions for the Apache web server.

        That particular part of the rewrite rules tells Apache to rewrite any requested URL that starts with www. to the same URL only without the www. part. The MODX line says "If the URL doesn't start with example.com, then rewrite it so that it does". The A2 version says "If the URL starts with www., then rewrite it so that it doesn't".

        The part that's important to MODX is the part that starts with "# The Friendly URLs part", since that's the part that tells Apache to load MODX's index.php file with the actual file requested as the HTTP query string if Apache can't find the file or can't find the directory that was requested. So if Apache sees "http://domain.com/about.html", it looks for a file named "about.html", then looks for a directory of that name, doesn't find it, so it rewrites the request to "index.php?q=about.html". That index.php file runs MODX, which uses the "q=..." part to know which resource to use.
          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
          • 40088
          • 708 Posts
          Thanks Bob, I'll try your example.

          I double posted? I didn't intentionally do it.

          I've had issues with posting recently, sometimes I'm unable to reply at all and now apparently I'm double posting without knowing I'm doing it.
            Todd
            • 3749
            • 24,544 Posts
            No, you didn't double post. That was someone else and I momentarily confused the two of you. I deleted that line while you were writing your reply. wink
              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
              • 40088
              • 708 Posts
              Good to know I'm not losing my marbles.

              Your example worked great, Bob. Thanks.
                Todd
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                <sigh> I should always check to see if BobRay is online before I reply; this early in my morning he's sure to type faster than I can wink
                  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
                  • 3749
                  • 24,544 Posts
                  LOL. My Parents forced me to take a touch typing class in the summer when I was in high school. I was the only guy in the class and I hated them for it at the time. They said I'd thank them later and I did.

                  I don't remember the teacher at all, but I remember that the world typing champion at the time was a woman named Stella Pajunas. There was a poster of her in the classroom. She typed 216 words in one minute in 1946. I just checked Google and was astounded to learn that her record still stands.
                    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
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    I type around 70WPM, but not before breakfast!
                      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