Quantcast
Viewing all articles
Browse latest Browse all 11

How to create a 301 redirect with .htaccess and Apache

If you’re using Apache as a webserver, you’ve probably heard of the strangly named file “.htaccess”.  This file allows for configuration of Apache in a multitude of ways, but the one that I want to focus on in this post is 301 URL rewriting.

What is a 301 redirect?
A 301 redirect is often defined as “a permanent redirect from one URL to another”.  In other words, if you have a website at http://mycooldomain.com and you want to permanently redirect it to http://myreallycooldomain.com, you can create a 301 redirect from the original URL to the new, more awesome one.  Afterward, anyone accessing the original domain would be redirected to the new URL.  Think of it like the “change of address” form that you would use to route your phyisical mail from an old address to a new one after a move.

Creating 301 redirects using .htaccess
The first step in creating 301 redirects using .htaccess is to locate the .htaccess file.  This file is going to be located in the root directory of your website.  If you are not running an Apache webserver, you will not have a .htaccess file nor can you create and use one.  Edit the .htacces file with the text editor of your choice.  I recommend Sublime or Notepad++, but you can use any text editor.

Once the .htaccess file is open in the text editor, read through the existing contents of the file.  Often times there is code in the .htaccess file that was added by another application (WordPress, Drupal, etc).  If the following lines do not appear in the .htaccess file, add them at the top.

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

These lines will enable the rewrite engine and allow Apache to redirect the URLs that you have in mind.  If you want to redirect from a specific page like index.html to another specific page like index2.html, you can add the following two lines.

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^index\.html/?$ index2\.html [NC,R=301]

Replace “domain\.com” with your domain information and replace “index\.html” and “index2\.html” with the original page and the page you want to redirect to, respectively.  You’ll need to add these two lines for each seperate file you want to redirect from and to.  If there you have a large number of these files, the Apache RewriteMap would likely be worth reading up on.

If you’re looking to create a 301 redirect at the directory level, you can add the following line:

RewriteRule ^/olddirectory /newdirectory [NC,R=301]

Replace “olddirectory” with the directory you want to redirect from and “newdirectory” with the directory you want to redirect to.

If you need to redirect a URL at the directory level but keep the URL path that follows, you can use the following line:

RewriteRule ^/olddirectory/(.*) /newdirectory/$1 [R=301,NC]

This is just the tip of the iceburg.  For more information on other fun things you can do with .htaccess, check out the resources below.

Additional resources:
About .htaccess
Apache Module mod_rewrite
Using Apache RewriteMap


Viewing all articles
Browse latest Browse all 11

Trending Articles