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. 301 redirect .htaccess

    301 redirect .htaccess

    Technical SEO Issues
    5 3 234
    • 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.
    • seobackbone
      seobackbone last edited by

      Hi guys

      I am working on some 301 redirects on an apache webserver and I'd like a bit of assistance in trying to get a specific type result:

      I want all addresses from domaina.com to be redirected to domainb.com in the same structure so domaina.com/folder/file will go to domainb.com/folder/file expect for 2 folders.
      ie:

      DomainA.com --> DomainB.com
      except domainA.com/folder1
      and domainB.com/folder2

      Can someone let me know how I can pull this off?

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

        It's a while since I've done htaccess rewrites, but I think that this is what you are after:

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

        What's going on there then:

        Line 1 turns the rewrite engine on. Don't repeat this if it is already there.

        The next two lines are looking for where the host name matches the old, in either www. or non www. format

        The final line then says to take the part of the address represented by the $ in the previous lines and put it at the end of the new domain.

        Test it of course, but I think that is what you are after.

        seobackbone 1 Reply Last reply Reply Quote 1
        • seobackbone
          seobackbone @matbennett last edited by

          Cheers for the fast response Matt will test it out however I was looking to also solve the 2nd part of my problem refering to the specific folders:

          === FROM PREVIOUS POST ====
          I want all addresses from domaina.com to be redirected to domainb.com in the same structure so domaina.com/folder/file will go to domainb.com/folder/file expect for 2 folders.

          ie:

          DomainA.com --> DomainB.com
          except domainA.com/folder1
          and domainB.com/folder2

          Any help is greatly appreciated

          thanks

          matbennett 1 Reply Last reply Reply Quote 0
          • matbennett
            matbennett @seobackbone last edited by

            Apologies - slightly misread it first time.  OK, something like:

            RewriteEngine On
            RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
            RewriteCond %{HTTP_HOST} ^www.olddomain.com$
            RewriteCond %{REQUEST_URI} !^/folder2/?$
            RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]

            We're probably though getting in to the realms of something I should test before I should post, but that is how you put exceptions in.

            1 Reply Last reply Reply Quote 1
            • Christy-Correll
              Christy-Correll last edited by

              Hi Stilianos, how did this work out for you?

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              • 301 redirects without .htaccess
                NancyVPS
                NancyVPS
                0
                5
                2.0k

              • Htaccess code to 301 redirect a folder change
                Paul_MC
                Paul_MC
                0
                4
                160

              • Do I need a 301 redirect on htaccess if Apache is already configured to serve?
                riplash
                riplash
                0
                6
                355

              • Delete 301 redirected pages from server after redirect is in place?
                RuthBurrReedy
                RuthBurrReedy
                0
                5
                4.0k

              • Help needed please with 301 redirects in htaccess file.
                petersommertravels
                petersommertravels
                0
                9
                2.3k

              • 301 Redirect Domain or 301 Redirect Domain + Interior Pages
                Room214
                Room214
                0
                7
                1.1k

              • 301 redirect .htaccess problem
                sferrino
                sferrino
                0
                4
                709

              • 301 Redirect?
                DiamondJewelryEmpire
                DiamondJewelryEmpire
                0
                3
                961

              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