We are committed to providing fast, efficient, and affordable software solutions that set new standards in the software development industry.
  • Using 301 Permanent Redirects on your Apache Web Server
Technology Articles > Software > Servers > Using 301 Permanent Redirects on your Apache Web Server

When moving your website to a new domain or directory, it’s important to redirect visitors on your old website to your new location. The primary reason for this is so that your visitors can still find your website and the content on your website, even if they haven’t updated their bookmarks. This goes for visitors who are just discovering your website via a link from another site as well. But redirecting old links to your new website location is also important for search engine optimization purposes. Each inbound link to your website improves your search engine ranking, and by redirecting your website traffic properly, you can move your website without sacrificing the “link juice” you’ve built up.

The best method for redirecting your old website to your new website is to use a 301 redirect. This tells browsers and search engines that your site has moved permanently. It also automatically sends visitors to your old address to the corresponding page on your new website.

If your website is hosted on an Apache server with the mod_rewrite module enabled (practically all Apache servers have mod_rewrite turned on), you can create a 301 redirect by editing your .htaccess file.

The .htaccess file is located in the root directory of your website. To create a 301 redirect, you’ll need to access the .htaccess file that’s in the root directory of your old website. If there isn’t one there, simply create one using Notepad or any other plain text editor.

Delete everything that’s already in your .htaccess file and replace it with the following code:

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

Make sure you replace “domain.com” for your new domain or new directory path. For example, if you were moving to newsite.com/blog, you would alter the code so it appears as:

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

Upload this .htaccess file to the root of where your old website used to be located. That’s all you have to do. Now, all users will be redirected to the correct domain name. So, for example, if someone navigates to http://www.olddomain.com/page1, they will automatically be taken to http://www.newdomain.com/page1.

Note that you do not have to retain the old pages on your old website. In fact, it’s better if you completely empty out the old directory and leave nothing but the .htaccess file. This will prevent your old content from being confused as duplicate content when you publish your new site.
Also, if you will only be temporarily moving your site, use a 302 redirect with the following code:

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

A server-side redirect is always better than an HTML browser-based META refresh forwarding method. Forwarding web pages using a META refresh has been abused by spammers in the past, and now may cause your site to demoted in search engines. The 301 redirect has the benefit of retaining all your inbound links (as long as the site structure at your new location is the same) while making it easy for users to find your new location.