The Moz Q&A Forum

    • Forum
    • Questions
    • My Q&A
    • Users
    • Ask the Community

    Welcome to the Q&A Forum

    Browse the forum for helpful insights and fresh discussions about all things SEO.

    1. SEO and Digital Marketing Q&A Forum
    2. Categories
    3. Technical SEO Issues
    4. .htaccess redirects

    .htaccess redirects

    Technical SEO Issues
    15 4 285
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as question
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • 3plains
      3plains last edited by

      I've done some research but can't find a good answer to this question. Here's my situation:

      Site redirects from example.com to www.example.com just fine. However, it doesn't work so well for internal pages.

      My site incorrectly redirects (non-www) example.com/page2 to www.example.com when it should instead go to www.example.com/page2

      So I need a method to redirect non-www internal pages to www versions. Currently I have this in my .htaccess - do I need to modify the rules?

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

      Thanks

      1 Reply Last reply Reply Quote 0
      • ThompsonPaul
        ThompsonPaul last edited by

        Replace you RewriteCond and RewriteRule with these 2 lines of code, Ryan:

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

        Paul

        3plains 1 Reply Last reply Reply Quote 0
        • 3plains
          3plains @ThompsonPaul last edited by

          Thanks for the tip Paul, but this method isn't helping me out. I still get:

          example.com/page2 redirecting to www.example.com

          The redirect result you've given me is what I've gotten from a few other methods as well. Perhaps there is something else at play. In my .htaccess file I have another Rewrite going on before the non www redirect we're discussing now. It is this:

          RewriteEngine on
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule . index.php [L]

          ......do you think this could be messing something up for me?

          MickEdwards 1 Reply Last reply Reply Quote 0
          • MickEdwards
            MickEdwards @3plains last edited by

            That last rule is the Wordpress default rule slightly trimmed:

            <ifmodule mod_rewrite.c="">RewriteEngine On
            RewriteBase /
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule . /index.php [L]</ifmodule>

            I would try this version and revert if no good.  I'd also install Fiddler and watch all the header responses to see if there is a clue.

            1 Reply Last reply Reply Quote 0
            • 3plains
              3plains last edited by

              Tried it, and same result as before.

              I've even tried this from SEOmoz:

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

              ....and it gave my server an error. I don't know I guess something funky is going on.

              MickEdwards 1 Reply Last reply Reply Quote 0
              • ThompsonPaul
                ThompsonPaul last edited by

                Ryan, it seems there must be a conflict in your htaccess file? If you're willing, you can PM me a copy of the full file and the site URL and I'll see if i can find anything.

                Paul

                3plains 1 Reply Last reply Reply Quote 1
                • MickEdwards
                  MickEdwards @3plains last edited by

                  i'd try disabling all plugins and test.  Also ensure in General >> Settings the URLs are including the www (I assume this is Wordpress). There seems to be some internal configuration causing this.

                  1 Reply Last reply Reply Quote 0
                  • 3plains
                    3plains @ThompsonPaul last edited by

                    Sent you an email.

                    1 Reply Last reply Reply Quote 0
                    • Aleyda
                      Aleyda last edited by

                      Hi Ryan,

                      For non-www to www redirects (for all the URLs under the domain) you can use:

                      RewriteEngine On
                      RewriteCond %{HTTP_HOST} !^www.
                      RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

                      Thanks!

                      3plains 1 Reply Last reply Reply Quote 0
                      • 3plains
                        3plains @Aleyda last edited by

                        Tried it,

                        Same results as above.

                        Aleyda 1 Reply Last reply Reply Quote 0
                        • Aleyda
                          Aleyda @3plains last edited by

                          It's there something else then in your htaccess? I also checked it with http://htaccess.madewithlove.be/ and worked fine. Could you please test it with another domain so you can take a look it's ok? I'm afraid that it might be something else that you have configured there and that's why it doesn't work since the code is alright.

                          1 Reply Last reply Reply Quote 0
                          • Aleyda
                            Aleyda last edited by

                            Hi again. I've just seen your answer above, that you also have the following:

                            RewriteEngine on
                            RewriteCond %{REQUEST_FILENAME} !-f
                            RewriteCond %{REQUEST_FILENAME} !-d
                            RewriteRule . index.php [L]

                            In your htaccess so I've added it before too:

                            RewriteEngine on
                            RewriteCond %{REQUEST_FILENAME} !-f
                            RewriteCond %{REQUEST_FILENAME} !-d
                            RewriteRule . index.php [L]

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

                            and again worked fine! Do you have something additional to that?

                            Thanks!

                            3plains 1 Reply Last reply Reply Quote 0
                            • 3plains
                              3plains @Aleyda last edited by

                              Hey Aleyda,

                              Thanks for the help, but unfortunately it still won't work. There are 2 other pieces of info in my .htaccess file.

                              1. Some PHP stuff (not sure what it does)

                              Use PHP 5.3

                              AddType application/x-httpd-php53 .php

                              2. 301 redirects I did a few days ago (about 30 articles that looks like this)

                              Redirect 301 /article http://www.example.com/blog/article

                              Redirect 301 /article2 http://www.example.com/blog/article2

                              ....It must be one of these two remaining issues...?

                              ThompsonPaul 1 Reply Last reply Reply Quote 0
                              • 3plains
                                3plains last edited by

                                Paul figured it out. You the man!

                                1 Reply Last reply Reply Quote 0
                                • ThompsonPaul
                                  ThompsonPaul @3plains last edited by

                                  The php declaration is to force your server to use php version 5.3, 3Plains. It's often put in place when a site's applications require a more recent version of php than is the default on the server.

                                  @Aleya - his htaccess had the php declaration in the middle of some of his conditionals, which I suspect was the issue. Had him move the php declaration to the top of the file before turning rewrite engine on. Seems to have resolved the issue.

                                  (Note, the php declaration can also be placed as last line in the file. I just find it better at the top so it reminds it's there in case I have a php version issue after a future server upgrade)

                                  Pleased we got ya working 🙂

                                  Paul

                                  1 Reply Last reply Reply Quote 1
                                  • 1 / 1
                                  • First post
                                    Last post
                                  • 301 redirect syntax for htaccess
                                    IOHanna
                                    IOHanna
                                    1
                                    3
                                    6.0k

                                  • Htaccess redirects
                                    Tom-Anthony
                                    Tom-Anthony
                                    0
                                    3
                                    220

                                  • Redirection in .htaccess
                                    Bharath_ATZ
                                    Bharath_ATZ
                                    0
                                    5
                                    91

                                  • .htaccess redirect question
                                    David-Kley
                                    David-Kley
                                    0
                                    4
                                    541

                                  • Help creating a 301 redirect in my htaccess file
                                    Felip3
                                    Felip3
                                    0
                                    4
                                    108

                                  • Redirect a 301 Redirect
                                    RedCaffeine
                                    RedCaffeine
                                    0
                                    5
                                    98

                                  • 301 redirect .htaccess
                                    Christy-Correll
                                    Christy-Correll
                                    0
                                    5
                                    234

                                  • Htaccess redirects how many can i have and does it slow site down
                                    ClaireH-184886
                                    ClaireH-184886
                                    0
                                    3
                                    1.2k

                                  Get started with Moz Pro!

                                  Unlock the power of advanced SEO tools and data-driven insights.

                                  Start my free trial
                                  Products
                                  • Moz Pro
                                  • Moz Local
                                  • Moz API
                                  • Moz Data
                                  • STAT
                                  • Product Updates
                                  Moz Solutions
                                  • SMB Solutions
                                  • Agency Solutions
                                  • Enterprise Solutions
                                  • Digital Marketers
                                  Free SEO Tools
                                  • Domain Authority Checker
                                  • Link Explorer
                                  • Keyword Explorer
                                  • Competitive Research
                                  • Brand Authority Checker
                                  • Local Citation Checker
                                  • MozBar Extension
                                  • MozCast
                                  Resources
                                  • Blog
                                  • SEO Learning Center
                                  • Help Hub
                                  • Beginner's Guide to SEO
                                  • How-to Guides
                                  • Moz Academy
                                  • API Docs
                                  About Moz
                                  • About
                                  • Team
                                  • Careers
                                  • Contact
                                  Why Moz
                                  • Case Studies
                                  • Testimonials
                                  Get Involved
                                  • Become an Affiliate
                                  • MozCon
                                  • Webinars
                                  • Practical Marketer Series
                                  • MozPod
                                  Connect with us

                                  Contact the Help team

                                  Join our newsletter
                                  Moz logo
                                  © 2021 - 2026 SEOMoz, Inc., a Ziff Davis company. All rights reserved. Moz is a registered trademark of SEOMoz, Inc.
                                  • Accessibility
                                  • Terms of Use
                                  • Privacy