Redirecting a URL on an Apache Web Server

Start learning on Udemy today!
If you need to redirect a URL from one webpage to another one, you can insert a meta refresh directive in the head section of the HTML code as shown below:

<META HTTP-EQUIV="Refresh" CONTENT="0; URL=/blog/newpage.html">

The content parameter specifies how many seconds to wait until the refresh directive redirects a visitor to the page to the URL that is specified by the URL parameter. In the example above, the redirect occurs immediately, since zero seconds are specified, but sometimes someone will want the visitor to read a note he has placed on the original webpage before redirecting the visitor, so might specify a nonzero value, e.g., 10 or 15 seconds.

Another way to redirect visitors and search engines that index the page is to use a server-side redirect by a .htaccess file placed in the directory where the original web page is located. A directive similar to the following one can be placed in the .htaccess file:

Redirect 301 example.html /somedirectory/example.php

In the example above, any vistor accessing example.html will immediately be redirected to example.php on the same server. The file example.php could be in the same or another directory as example.html. The visitor could also be redirected to a webpage on another webiste, e.g., the following could be used:

Redirect 301 example.html

When the redirect is placed in the .htaccess file, the visitor's browser will never see the original example.html file, which could be deleted if it is no longer needed. The 301 code tells search engines that the webpage is being permanently redirected to the new page. This technique is useful if you wish to have a page's ranking carry over to the new page, since search engines such as Google will carry over page rank to the new page if this status code is used.

A 302 code can be used, instead, to indicate to a search engine that the page is being temporarliy redirected. E.g., that might be done to redirect visitors while a webpage is undergoing maintenance.

Multiple redirects can be included within the same .htaccess file. E.g.:

Redirect 301 /somedirectory/example1.html /someotherdirectory/example1.html
Redirect 301 /somedirectory/example2.html /someotherdirectory/example2.html

Note: when I've omitted the full path in relation to the root of the website from the original file names when I've included multiple redirects that I have observed inconsistent redirects, so I've included that path in the example above. Omitting the path for the pages to which visitors are to be redirected results in a "500 Internal Server Error" being displayed, instead of the desired webpage.

In order for the .htaccess method to work, an AllowOverride directive must be placed in Apache's httpd.conf file in the virtual host section for the website that is going to use the redirect. If that is not done, a visitor will see a "500 Internal Server Error" page, instead of either example.html or example.php.

And, if you looked in the Apache error log file for the website, you would see an entry similar to the following, if the .htaccess file was in the directory /home/jdoe/public_html/blog/.htaccess:

/home/jdoe/public_html/blog/.htaccess: Redirect not allowed here

For the redirect to work, an AllowOverride directive must be placed in Apache's htpd.conf for the relevant website. E.g., if there was a virtual host section in the httpd.conf file for the website and the directory in which the original file resides and in which the .htaccess file is placed is /home/jdoe/public_html/samples, then the following lines could be placed in the virtualhost section for in httpd.conf:

<directory /home/jdoe/public_html/samples>
   AllowOverride FileInfo

One could also use "AllowOverride All", since "AllowOverride FileInfo" allows only a subset of what "AllowOverride All" allows.

After updating the httpd.conf file, the Apache webserver software must be restared, which can be done with the command apachectl restart. Editing the httpd.conf file and restarting Apache will likely require root access, so, if you are using a hosting provider, unless an AllowOverride directive is already specified for your website, which may be the case, you might not be able to use this method.

The httpd.conf file will also need to support a rewrite engine, e.g., by using mod_rewrite. If the following line is in the Dynamic Shared Object (DSO) Support section of the httpd.conf file, then the mod_rewrite module will be loaded. If you are using a hosting provider, this may already be present.

LoadModule rewrite_module modules/


  1. Permanent Redirect with HTTP 301
    By: Shailesh N. Humbad
    Created: April 4, 2005
    Last Modified: July 18, 2013
    Somacon - articles on web development, software, and hardware
  2. HTTP/1.1 Status Code Definitions: Redirection 3xx
  3. Apache HTTP Server Tutorial: .htaccess files
    Apache HTTP Server Project
  4. How do I redirect one Web page URL to another?
    Tiger Technologies - Support Start Acronis True Image 2014 with FREE 5GB cloud storage! Buy now for $49.99 only! Put your passion online! 15% off new products at GoDaddy!

Valid HTML 4.01 Transitional

Created: Sunday March 9, 2014