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. Web Design
    4. What is the code to 301 http to www in htaccess file on unix server

    What is the code to 301 http to www in htaccess file on unix server

    Web Design
    3 3 150
    • 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.
    • Ron10
      Ron10 last edited by

      i want to 301 my http home page  to www on a linux server and all my other redirects are set up similar to  this in my htaccess file:

      redirect 301 /example-page.html http://www.example-page.html

      how do I 301 redirect:

      http://example.com to http://www.example.com

      I've tried all kinds of code recommended for an htaccess file on a linux server  and nothing seems to work.

      Thanks for the help mozzers!

      Ron

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

        Hi Ron,

        You will need to use URL rewrite and the following code:

        RewriteEngine On

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

        That will redirect any page/file on example.com/file to www.example.com/file with a 301 redirect.

        Hope that helps!

        1 Reply Last reply Reply Quote 1
        • MoosaHemani
          MoosaHemani last edited by

          1st works fine for me....

          Redirect 301 /tags/xp/ http://www.test.com/tags/windows-xp/

          if it is not working there must be some other problem!

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          • Cost Difference Between Coding Custom Theme or Coding Child Theme
            brooksmanley
            brooksmanley
            0
            4
            47

          • What are the downsides and/or challenges to putting page paths (www.example.com/pagepath) on a different server?
            HiveDigitalInc
            HiveDigitalInc
            0
            2
            37

          • HTTPS redirecting to subdomain; do I need to 301 to HTTP first?
            alimo
            alimo
            0
            5
            346

          • Coding breadcrumbs manually
            Jon-C
            Jon-C
            0
            3
            200

          • SEO and Server Connectivity....
            RobertChapman
            RobertChapman
            0
            4
            488

          • WWW or no WWW?
            Mr.Rangen
            Mr.Rangen
            0
            4
            454

          • Is there a limit for 301 redirection in htaccess file?
            Vizergy
            Vizergy
            0
            3
            5.3k

          • Testing your code and site
            JohnW-UK
            JohnW-UK
            0
            5
            2.1k

          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