This post will show how to use .htaccess to move a website to a new domain, especially when using Bluehost.
A couple of days ago, as I set up this website, jonathanlee.org, I struggled to set up a 301 redirect from my previous domain, jonathanleeonline.com. My hosting is with Bluehost, and they offer a domain redirection tool – but it does not work with addon domains.
Specifically, if I own both oldsite.com and newsite.com, and both are registered with a single bluehost account, oldsite.com is the primary domain, and newsite.com is an add-on domain – and is set up as newsite.oldsite.com (although newsite.com also works). It’s stupid.
But as a result, Bluehost’s regular domain redirection tool and does not work. Technical support tells me I have to modify my .htaccess file, but refuses to give any guidance on how to do so.
I couldn’t find any turn-key solutions via Google searching, so I did some researched, and wrote my own .htaccess solution. And I’m posting the solution online, to help out others.
This is my .htaccess file (by default, in the public_html folder for Bluehost):
# Use PHP5.4 as default AddHandler application/x-httpd-php54 .php RewriteEngine On RewriteBase / RewriteRule ^wp-admin.*$ - [L] RewriteRule ^wp-login\.php$ - [L] RewriteCond %{HTTP_HOST} ^jonathanleeonline.com$ [OR] RewriteCond %{HTTP_HOST} ^www.jonathanleeonline.com$ [OR] RewriteCond %{HTTP_HOST} ^jonathanlee-org.jonathanleeonline.com$ [OR] RewriteCond %{HTTP_HOST} ^www.jonathanlee-org.jonathanleeonline.com$ [OR] RewriteCond %{HTTP_HOST} ^www.jonathanlee.org$ RewriteRule ^(.*)$ "http\:\/\/jonathanlee\.org\/$1" [R=301,L] # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress
The first part, #Use PHP5.4, can be ignored. The part between #BEGIN WordPress and #End WordPress should also be ignored – that’s default WordPress code.
The middle part is the most important part. It says, first of all, to ignore all requests to access the WordPress admin files on the old website. That way, you can still log into the WordPress control panel. The way this .htaccess file works, if it sees that you are trying to access my oldsite.com/wp-admin/index.php (the WordPress Dashboard), it does nothing (the dash means do nothing), then stops checking (the [L] means exit loop).
Next, it checks to see if you’re accessing either oldsite.com, www.oldsite.com, newsite.oldsite.com or www.newsite.oldsite.com, and redirects everything to newsite.com.
Most importantly though, this .htaccess file includes wildcards. This way, if you’re trying to access www.oldsite.com/mypage.html, the code will automatically 301 redirect you to newsite.com/mypage.html!
Here’s a generalized version of the code, which you can copy and paste and use yourself:
# Use PHP5.4 as default AddHandler application/x-httpd-php54 .php RewriteEngine On RewriteBase / RewriteRule ^wp-admin.*$ - [L] RewriteRule ^wp-login\.php$ - [L] RewriteCond %{HTTP_HOST} ^oldsite.com$ [OR] RewriteCond %{HTTP_HOST} ^www.oldsite.com$ [OR] RewriteCond %{HTTP_HOST} ^newsite.oldsite.com$ [OR] RewriteCond %{HTTP_HOST} ^www.newsite.oldsite.com$ RewriteRule ^(.*)$ "http\:\/\/www\.newsite\.com\/$1" [R=301,L] # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress
Great Success!
Hey! This post could not be written any better! Reading this post reminds me of my
previous room mate! He always kept talking about this. I will forward this write-up
to him. Fairly certain he will have a good read. Thank you
for sharing!