How to do 301 redirect to new domain via htaccess?

Discussion in 'Programming' started by DomainMagnate, Sep 26, 2015.

  1. #1
    I've switched domains and would like to redirect all pages like olddomain.com/somepage.html to newdomain.com/somepage.html - while preserving the same url structure. Could you show the htaccess code for it? thanks!
     
    DomainMagnate, Sep 26, 2015 IP
  2. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,296
    Likes Received:
    1,700
    Best Answers:
    31
    Trophy Points:
    475
    #2
    I haven't used this, just have it on file in case I need it for the same scenario. It's rather simplified, but it should redirect any requests on the old domain to the same location on the new domain.

    Redirect 301 (.*)$ http://www.newdomain.com/$1
    Code (markup):
     
    qwikad.com, Sep 26, 2015 IP
  3. DomainMagnate

    DomainMagnate Illustrious Member

    Messages:
    10,932
    Likes Received:
    1,022
    Best Answers:
    0
    Trophy Points:
    455
    #3
    just one line? Wouldn't that create some loop?
     
    DomainMagnate, Sep 27, 2015 IP
  4. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,296
    Likes Received:
    1,700
    Best Answers:
    31
    Trophy Points:
    475
    #4
    Nothing is going to loop, but if you look for a SEO friendly (which really means very little today) way of doing the same thing, do this:

    RewriteEngine on
    RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
    Code (markup):
    The rule above redirects each page on an old domain to the same pages on a new domain.
     
    qwikad.com, Sep 27, 2015 IP
  5. Keteki

    Keteki Active Member

    Messages:
    112
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #5
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
    RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
    Code (markup):
     
    Keteki, Oct 9, 2015 IP