Change Your Domain Name and Keep Your Incoming Links With .htaccess And mod_rewrite
Posted on September 12th, 2007 in Web Development, Apache, Code |
When moving our site from ablogapart.org to michaelkrol.com, this handy little bit helped move our entire website with four lines of code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)ablogapart.org [NC]
RewriteRule ^(.*)$ http://michaelkrol.com/$1 [R=301,L]
This reference was extremely helpful.
This RewriteRule lives in the .htaccess file at ablogapart.org and tells any request coming in to that domain to swap ablogapart.org out and replace it with michaelkrol.com. This includes ANYTHING after the trailing slash, like a direct link to a previous post. So http://ablogapart.org/this/direct/link gets sent properly to http://michaelkrol.com/this/direct/link
The important part is the [R=301] which sends a 301 (Permanent) Redirect header. That tells search engines that the page has moved permanently.
Just imagine what you’d have to go through setting up individual forwarding links…