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. How should I properly setup my .htaccess file?

    How should I properly setup my .htaccess file?

    Technical SEO Issues
    5 3 2.0k
    • 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.
    • maxduveen
      maxduveen last edited by

      I have searched google for 'how to setup .htaccess file' and it seems that every website has some variation. For example:

      RewriteCond %{HTTP_HOST} ^yoursite.com

      RewriteRule ^(.*)$ http://www.yoursite.com/$1 [R=permanent,L]

      On SEOMOZ someone posted this:

      RewriteCond %{HTTP_HOST} !^www.yoursite.com [NC]

      RewriteRule (.*) http://www.yoursite.com/$1 [L,R=301]

      On yet another website, I found this:

      RewriteEngine On

      RewriteCond %{HTTP_HOST} !^your-site.com$ [NC]

      RewriteRule ^(.*)$ http://your-site.com/$1 [L,R=301]

      As you can see there are slight differences.

      Which one do I use? I'm on Apache CentOS and I have HTML5 websites and several Joomla! wesites. Would the HTACCESS File be different for both?

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

        There are many many things you can do with your .htaccess

        What are you trying to accomplish? Are you trying to redirect non-www to www? Trying to get a better link structure? Trying to redirect pages? Or a site?

        In most cases it depends on your server setup and your needs. Since you mentioned Joomla, this post has some pretty good explanations and examples at the bottom you can download.

        maxduveen 1 Reply Last reply Reply Quote 0
        • maxduveen
          maxduveen @CaseyKluver last edited by

          Thanks for the reply Casey!

          I'm trying to redirect non www to www. and I'm on a dedicated server running apache. For my HTML5 pages, what should my HTACCESS file look like.

          My ultimate goal is to be SEO optimized. Any other settings that will help my sites would be appreciated 😄

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

            Hi Max

            1  should work for your requirements. It is what we use to direct all non www traffic to the www page on 3 different platforms. Oscommerce, php custom build, Wordpress.

            Just verify everything by trying to hit a couple none www urls to verify everything re-directs okay.

            Also here is a good htaccess shortcut sheet (Note site sometimes loads slow so you may want to copy and paste it to a text file for later reference)

            Hope that helps

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

              Well, after trying out all 3 of the options i posted, none worked and one threw a server error.

              BUT, alas... I found the correct way to do it:

              <code class="htaccess" title="in your .htaccess file">RewriteEngine On
              RewriteCond %{HTTP_HOST} !^www\.
              RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]</code>
              
              This is the correct way to forward all non www requests to www.yoursite.com
              

              No need to put your domain name, simply add the code above to a .htaccess file on your server and viola!

              If someone wants to redirect all www to non www then the code would be:
              ```
              
              > ```
              > ```
              > <code class="htaccess" title="in your .htaccess file">RewriteEngine On
              > RewriteCond %{HTTP_HOST} !^my-domain\.com$ [NC]
              > RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L]</code>
              > ``` 
              > ```
              
              ```
              ```
              <code class="htaccess" title="in your .htaccess file">and of course replace the my-domain with your domain ;)</code>
              ``` 
              ```
              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              • Where to put 301 redirects in my Wordpress htaccess file?
                Benspain
                Benspain
                0
                3
                34

              • Htaccess file help
                DirkC
                DirkC
                0
                4
                120

              • What's Worse - 404 errors or a huge .htaccess file
                customerparadigm.com
                customerparadigm.com
                0
                3
                304

              • Can someone interpret this entry in my htaccess file into english so that I can understand?
                cathibanks
                cathibanks
                0
                17
                199

              • Do i have my robots.txt file set up properly
                ClaireH-184886
                ClaireH-184886
                1
                4
                319

              • Kill your htaccess file, take the risk to learn a little
                KevinBudzynski
                KevinBudzynski
                0
                2
                373

              • Need Help writing 301 redirects in .htaccess file
                Dr-Pete
                Dr-Pete
                0
                5
                1.3k

              • Converting files from .html to .php or editing .htaccess file
                donford
                donford
                0
                11
                3.6k

              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