How to redirect visitors and search engines when you change a website URL

Over the years I’ve built many websites, some for friends, family and many for myself. Blogs, just like this one, that over time grow and adapt to new subjects. Most of the time the original URL I chose lasts the entire life of the site but sometimes I decide to change the URL, move the website to a new more fitting address, maybe one that I saved up to buy or one that just fits better. At times like this it’s very important to tell visitors and search engines where to go in order to access all of the old content at the new address.

When you build a website several things happen that give it credibility and value over time:

  • You tell your friends where to go to read your posts
  • You link to your site from forums and other websites like Facebook or Twitter
  • Search engines notice your site and begin indexing it, recording it in their dynamic listings
  • There may be physical copies of your old URL on business cards or advertisements

When you change the URL of your website it’s not quite like changing the name of a physical business, it’s more like moving the business across town and changing it’s name all at once! You can expect business to drop way off as customers adapt to the change. With websites what you want to do is leave a note at the old address automatically redirecting visitors to the new site and telling search engines “hey, this site content hasn’t gone away it has just moved permanently” and many times the search engine will take this notice and transfer some of the reputation to the new address. What you need to do this right is use a 301 redirect. While we’re on the subject, you might also want to register that new URL for 3+ years so that search engines recognize your long term commitment to the new address.

301 redirects tell search engines that the site they are trying to visit has moved permanently. At the same time, the redirect will automatically send visitors to the new URL, even if they typed in the old one. But not all 301 redirects are created equal, some will take all of the addresses from your old site such as the homepage, contact page, news pages, product pages etc. and redirect all of them to the homepage of your new site URL. That’s not so great because it destroys the value that each unique web page had along with the convenience built in to all of the old links and search engine listings you’ve got. It’s like saying “hey I moved and my house is in this neighborhood but you’ll have to find the exact house yourself now”.

A better kind of 301 redirect uses a wildcard to automatically match the old address to the new one. So imagine having and you’ve just setup and basically it’s the same contact page just at a new site URL. So you see here, the page hasn’t changed, the name of the page in the URL hasn’t changed, just the URL itself. You can use this wildcard redirect to automatically send users who wanted the contact page at the old site right to the new address contact page. To do this we will use the .htaccess file at the root directory of the old site URL along with mod rewrite and the wildcard match to redirect visitors. Just follow the steps below or leave a comment for help.

When you wan to redirect one URL to another one but keep all of the files and folders the same this is what you do, and yes, you can expand the new site, but this will keep the old paths updated for any old links and search engine listings:

  1. In the root directory of your old site URL find or create a file called .htaccess and then rename it to htaccess.html (this will make it easier to edit as most system files begin with a . and sometimes go invisible, changing the name won’t hurt the file and we’ll fix it later)
  2. Download the htaccess.html file and edit it on your local machine with an HTML program like Dreamweaver or you can use Text Edit, just don’t use Word as it will reformat characters sometimes
  3. If there is any content in the file you might want to set it aside as a backup, if you created the file fresh then just paste the following lines of code:
    RewriteEngine On
    RewriteRule ^(.*)$1 [R=301,L]
  4. These two lines tell the server to turn on mod rewrite and then enact the wildcard match (.*) so anything after the old url will be preserved and then pasted at the new URL where the $1 is. Be sure to replace with the name of your site
  5. Save and upload the htaccess.html file to your old site root directory and then change the name back to .htaccess you can test that it worked by typing in one of the URL’s at your old site and seeing if it redirects you to the new site

Tons of cool things can be done with .htaccess files, you can completely update and change the way files are accessed on your server to make them shorter or more descriptive. Here are a few great resources for you to explore if you want to learn more. Server Fault All About Mod Rewrite, Wikipedia URL Redirection, htaccess tips and tricks

Here’s a couple more mod rewrite rules I use sometimes. The first one tells incoming requests to always change to http:// instead of http://www which is neat because the www are extra characters that might take up space in Twitter. Also, notice at the end in the brackets there is an L, this means it is the last argument to be read.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

I use this next rewrite command to simplify older archive URL’s I used to use on a site. After a few years I decided the URL’s were too long and I didn’t want so many folders to show up. I changed the way my content management system published the pages but I also wanted to help visitors and links to make it to the new pages. What this URL says is take any URL that is trying to go to /archives/number/number/wildcard.html and send it to wildcard so imagine /archives/10/2001/page-name.html now it will just go to /page-name/ the $3 means “take the third argument and reproduce it here” and arguments are surrounded with parentheses like this ()

RewriteEngine On
RewriteRule ^archives/([0-9]{4})/([0-9]{2})/(.*).html$3 [R=301,L]