Using .htaccess to redirect from one domain to another

14,209

Solution 1

You need to add commands like this to your .htaccess file:

redirect permanent /some-article/ http://www.example.com/some-article/

Is this a server with mod_rewrite? In this case you could do a generic redirection for all paths:

RewriteEngine On

RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]

Solution 2

I would modify your existing rewrite block to look like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# New code to redirect from example.net to example.com
# Permanent redirect for caching purposes, also include the query string
RewriteCond %{HTTP_HOST} ^example\.net
RewriteRule (.*) http://example.com/$1 [R=permanent,QSA,L]

# New code to redirect from www.example.net to www.example.com
RewriteCond %{HTTP_HOST} ^www\.example\.net
RewriteRule (.*) http://www.example.com/$1 [R=permanent,QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

AddHandler php5-script .php

Note that I haven't actually tested this...

REMOTE_HOST may also work instead of HTTP_HOST. The mod_rewrite documentation recommends using HTTP_HOST, although this will not work if the brower only understands HTTP/1.0

Share:
14,209
Philip Morton
Author by

Philip Morton

Updated on June 05, 2022

Comments

  • Philip Morton
    Philip Morton almost 2 years

    I recently purchased a new domain for my WordPress site and I want to redirect anyone who visits using an old domain to the new one. I haven't moved servers, just added a new domain.

    For instance, if they went to either of these:

    http://www.example.net/some-article/
    http://example.net/some-article/
    

    Then I'd like them to be redirected to the appropriate URL:

    http://www.example.com/some-article/
    http://example.com/some-article/
    

    How would you do this simple .net -> .com redirect with a .htaccess file? Any rule should apply to all URLs under the .net domain.

    Thanks in advance.


    Edit: I already have the .htaccess file on the server:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress
    
    AddHandler php5-script .php