Apache Server tutorials menu barAITech Solutions Home   Site Map   Support   Contact Us   Website & Servers 
Apache Tutorial Topics:   Apache Server and http.conf introduction,   Apache Redirection guide


Redirect URL's, Index, Directories, Ports, File types and more!



This easy to understand guide with useful examples will show you how to redirect virtually anything on an Apache server.
Unlike most websites, we describe in detail how the redirections and regex works so you can customize your redirects!
 
When would I need to use URL redirection on my web site?
1) Say a requestor (Browser, SE Bot, etc..) wants to "GET" a web page it knew about previously from your server but
it has been moved or renamed causing a page not found 404 error. To rectify this issue you need to 301 redirect
the request to the correct URL.

2) Fix canonicalization issues with your site so that there is only one URL for your site and home page you would redirect
the www to the non www version (or the otherway around) and redirect your home page to the domain root.

3) Maybe you need requests to redirect a URL to another port. These are all good reasons to use Apache's built in
redirection directives like Redirect, RedirectMatch or the all powerful mod_rewrite engine.


What method you choose depends on whether you want to satisfy the request with or without the requestors knowledge,
whether the change is temporary or permanent, and what resource(s) you are trying to redirect.

The following methods can be implemented in the Server, Directory, Virtual Host or .htaccess contexts.

Redirect permanent (301)

If you need to permanently redirect a URL or resource you can use the "Redirect permanent" or "Redirect 301"
directive. This is the redirection code\type of choice for a search engine friendly solution.

The format of this (and most of the following) directives is as follows:

Redirect [status] URL-Path URL

Be very careful! - The 302 redirect is the default for the Apache Server . If you do not specify
a valid parameter in the second  or status position, then a 302 temporary redirect will be used!

When would I want to use Redirect Pemanent?

1. If you have to permanently rename files on an existing server and want to inform the requestors (Browsers,Bots, etc..)
of this change.

Example: Renaming a .shtml (SSI Processing) file to .html or rename a .pl (perl) file to a .php file.

Redirect permanent /mypage.shtml http://my.server.com/mypage.html
Redirect 301 /myscript.pl http://my.server.com/myscript.php

Note that above you can use either the permanent or the 301 argument values to specify the type of redirection.


NOTE: If you have spaces in the source URL you wish to redirect, then enclose the URL in double quotes like this:
Redirect 301 "/my script.pl" http://my.server.com/myscript.php


2. You need to permanently move all pages in one directory to a new directory further down the tree from the document
root.

Example:
 You want to move all HTML tutorial pages from http://my.server.com/tutorials to http://my.server.com/tutorials/html

Redirect /tutorials/ http://my.server.com/tutorials/html/


3. Your server is a virtual hosting server and you want to permanently move a limited number of specific pages or
a complete directory from one domain to another.
Example:  Move one page http://my.bigdomain.net/networking/sales/ to http://my.store.com/networking

On the http://my.bigdomain.net site
Redirect /networking/sales/routerlist.html http://my.store.com/networking/routerproducts.html

Example: Move all pages from http://myfavorite.animals.com/sharks/protected/ to http://save.thesharks.org/

In the .htaccess on http://myfavorite.animals.com
Redirect /sharks/protected/ http://save.thesharks.org/



Redirect Temporary (302)

Redirect temporary works just like the Redirect permanent directive described above with a very important difference.

Caution - The 302 redirect can be handled diferently by various search engines and its use could seriously affect
your ranking over time in a negative way. This has changed for the better for the most part over time but your
results may vary. Use 302 redirects only when you have a good reason to do so.

The 302 redirect is the default for the Apache Server. If you do not specify a valid parameter
in the second or status position, then a 302 temporary redirect will be used!

When would I use a Redirect temporary directive?

It may be used on an intranet server where the redirected resource is not visible in the public domain or if Search
Engine indexing and page rank (PR) is not a concern and you don't want the requestor to "Remember" the redirection.

Examples:

1. You have a page on a new intranet server and you want to temporarily replace the home page until you get
the site in place. 

Redirect temporary /index.html http:/myintranet.server.com/underconstruct.html

2. You are moving this (or more) page(s) to a new domain and you want to test that the redirection is working
as desired first before informing the requestor the change is permanent. ! This is highly recommended procedure! If
by accident you misdirect, you don't want search engines to "remember" the mistake. Some search engines have a
long memory and it could take a month or more to get the cached mistake removed from the search index. Your mileage
may vary. 

You are moving a page puppycare.html on www.bestpetstore.com to a new domain www.bestanimalcare.com

Redirect 302 /dogs/puppycare.html http://www.bestanimalcare.com/dogs/puppycare.html 

Note that above you can use either the temporary or the 302 argument values to specify the type of redirection.


Redirect gone (410) or force "Page does not exist" (404)

The Redirect gone or Redirect 410 is used to indicate a previously known URL or resource permanently no longer exists.

This would normally be used with the intent to inform a user or search engine that the resource is gone and not just
"misplaced". Sometimes this has the effect of removing a page from a search index quicker than a 404.
*Note: To pattern match a group of URLs you can use RedirectMatch. For syntax see RedirectMatch in the next section.

*Some search engines have a URL removal tool that similarly can be used to remove URL's from an index but work faster.
Here is a link regarding Google's tool:
How do I use Google's URL removal request tool
 
Note: By using a 410 response or a URL removal tool you will lose any value (PageRank) this page has gained, It is
recommended to use a 301 redirect to the most appropriate page to help both users and retain search engine PR. 

Usage example:
You put up your web site "early" and have a under construction subpage. As this page has no value you want it
dissasociated from your site.

Redirect gone /underconstruct.html

Usage example:
You want to force a 404 response for a page even if it could possibly exist (Autogenerated by CMS, Blog, Forum, etc..).
Useful for page removal tools that require a page to return a 404 header response as a requirement.

Redirect 404 /redirect-tutorial.php?ID=20


RedirectMatch Permanent (301)

If you have more complex permanent redirection needs than the standard redirect directive, you can use RedirectMatch
permanent or "RedirectMatch 301".

The format of the RedirectMatch is as follows:
RedirectMatch [status] regex URL  Where regex is a regular expression. [Status] can be 301 or permanent.

Be very careful! - The 302 redirect is the default for the Apache 2.3 Server. If you do not specify a valid parameter
in the second  or status position, then a 302 temporary redirect will be used!

Lets demonstrate by example.

Assume the current servers URL is http://my.domain.com

Using this RedirectMatch command:
RedirectMatch Permanent ^/$ http://your.domain2.com/index.html

This will redirect all requests for http://my.domain.com root index (/)) to http://your.domain2.com/index.html


RedirectMatch Temporary (302)

RedirectMatch temporary or RedirectMatch 302 works exactly the same as the Redirect Permanent directive above with
one very important difference. It indicates to the requestor this is a temporary redirection and has the same SE related
issues as the Redirect temporary directive.

Once again,
Caution -  Using the 302 redirect can be handled diferently by various search engines and its use could seriously
affect your page rank in a negative way. This has changed for the better for the most part over time but your
results may vary. Use 302 redirects at your own risk.


Using ServerAlias for redirection

The Apache ServerAlias directive is used to point other URLs to the specified virtual host. When the alias is browsed
the canonicalized, "main" or aliased domain will be shown. In fact the address bar will not show a change
and thus "look" like a 302 redirect but the ServerAlias does not provide HTTP redirection

Mod_Rewrite - Domain Redirect

Mod_rewrite is a powerful extension module that picks up where RedirectMatch left off.  
This topic deserves a space of its own so, I will just touch on it here for the sake of completeness and give a
couple of useful examples.

You can use mod_rewrite in both the Server, Virtual Host and the .htaccess contexts. 

Example 1.

You are moving to a new domain hosted on the same or another server and want to permanently redirect all requests
for all pages with a one to one URI\pagename correlation from my.server1.com to my.server2.com

Method 1

Note that for this example and for this purpose, the .htaccess file needs to be in my.server1.com's server root

---------------------------------------------------------------------------
RewriteEngine On
RewriteRule ^(.*)$ http://my.server2.com/$1 [R=301,L]
---------------------------------------------------------------------------
For use in .htaccess and if it is not set globally or for the root directory of your domain, be sure to set
Options +Indexes +FollowSymLinks as needed before the RewriteEngine On directive.
Also, depending on your server configuration you may need to use RewriteBase. Typical usage is RewriteBase /
placed just after the RewriteEngine On directive. More on RewriteBase later.

RewriteEngine on tells apache to use mod_rewrite to process the following commands.

In RewriteRule:


Example 2 - The wrong way to do it - OR - Welcome to error 500 internal server error

Now lets say you need to make sure that all requests for your site go to the www version (on the same server).
e.g. You want to permanently redirect all requests from the non-www  example.com URL to www.example.com.

Given Example 1 you would think you could use the following:

Method 1
---------------------------------------------------------------------------
RewriteEngine On
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
---------------------------------------------------------------------------
For use in .htaccess and if it is not set globally or for the root directory of your domain, be sure to set
Options +Indexes +FollowSymLinks as needed before the RewriteEngine On directive.
Also, depending on your server configuration you may need to use RewriteBase. Typical usage is RewriteBase /
placed just after the RewriteEngine On directive. More on RewriteBase later.

With this configuration, the process works like this.
A get request is received and directed to host example.com by DNS

The RewriteRule is processsed and since evaluation of ^(.*)$ matches any "get" value this value becomes
backreference $1 and the URL is rewritten to www.example.com.

This is what we wanted, Right?
Well, so far.
After the rewrite the new URL is re-injected into server processing but guess what happens?
Since rewrite is on and www.example.com is hosted on the current server, it gets processed by RewriteRule again! and
again! and again! ... looping until the dreaded server error 500 occurs! 

Now you have a nonfunctional web site. Not what you wanted eh? Lets see how we fix that in Method 2.

Method 2 - How to properly redirect a non-www URL to the www version

This is a typical SEO tip used to help with PageRank consolidation as both the www version and the non-www version
are normally considered two different sites. This also insures that all requestors are responded to correctly no
matter which domain version is used. 

-------------------------------------------------------------------------------
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
-------------------------------------------------------------------------------
For use in .htaccess and if it is not set globally or for the root directory of your domain, be sure to set
Options +Indexes +FollowSymLinks as needed before the RewriteEngine On directive.
Also, depending on your server configuration you may need to use RewriteBase. Typical usage is RewriteBase /
placed just after the RewriteEngine On directive. More on RewriteBase later.

This works as method one but with the addition of a qualifying condition using Mod_Rewrite negation to prevent looping.

Here is the breakdown of the differences with method 1.
Here is the flow.
The first half of RewriteRule is processsed and since evaluation of ^(.*)$ matches any "get" value, this value
becomes backreference $1 and rewrite processing continues.

RewriteCond is processed next and if the requested host equals www.example.com the condition fails and no rewrite
takes place. If on the otherhand the the requested host does not match www.example.com the second half of
RewriteRule is applied and the URL is rewritten.

This is one of two preferred methods to used for redirection of the non-www request to the www site. This method, due
to the use of the negative logic in the RewriteCond statement, redirects even bogus absolute url GET requests.

Some of you may be wondering, Can't I just use positive logic for the redirection matching?
Sure! Here is the example: 

Method 3 - Alternate method to redirect a non-www URL to the www version using positive logic

Note: If you want to use other subdomains or hosts besides the www on the same server you will need to implement
this type of conditional logic.

-------------------------------------------------------------------------------
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
-------------------------------------------------------------------------------
For use in .htaccess and if it is not set globally or for the root directory of your domain, be sure to set
Options +Indexes +FollowSymLinks as needed before the RewriteEngine On directive.
Also, depending on your server configuration you may need to use RewriteBase. Typical usage is RewriteBase /
placed just after the RewriteEngine On directive. More on RewriteBase later.

Redirecting the www version of your domain to the non-www version

For those of you that prefer to use the non-www version of your domain here is how you can redirect requests
for the www version. 

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

For use in .htaccess and if it is not set globally or for the root directory of your domain, be sure to set
Options +Indexes +FollowSymLinks as needed before the RewriteEngine On directive.
Also, depending on your server configuration you may need to use RewriteBase. Typical usage is RewriteBase /
placed just after the RewriteEngine On directive. More on RewriteBase later.


Example 3 - How to redirect your home page to your domain URL

If you want to redirect individual files, usually Redirect or RedirectMatch is all you need.
If you want to redirect your home or index page to your domain URL, a common SEO recommendation, a normal
redirect can throw your server into a loop rewarding you with the dreaded 500 Server Error.
To do this correctly we need the additional capabilities available in mod_rewrite.

Here is the code that can go in the virtual host container, domain root directory container or its .htaccess file
--------------------------------------------------------------------------------------------------------
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[c-t]{3,9}\ /index\.html\ HTTP/ [NC]
RewriteRule ^(.*)index\.html /$1 [R=301,L]
--------------------------------------------------------------------------------------------------------
In the above example be sure to change the index\.html to whatever your home page is named.
For use in .htaccess and if it is not set globally or for the root directory of your domain, be sure to set
Options +Indexes +FollowSymLinks as needed before the RewriteEngine On directive.
Also, depending on your server configuration you may need to use RewriteBase. Typical usage is RewriteBase /
placed just after the RewriteEngine On directive. 

Note: RewriteBase / - (Sometimes needed) Used to set the URL base on per-directory (.htaccess) context when the
subustitution (what you are rewriting to) does NOT start with http:// (like the above example) and the actual physical
path does not match the request. You may run into this in some situations where an aliased directory or ScriptAlias
are used. Clue: If RewriteBase is needed, you may get 404 not found errors without it. Check the path shown in your
server logs.
For more information see Apache 2.2 documentation on RewriteBase

With that out of the way, now for the fun part. Just how does this work?
Thanks for asking!
First, notice the server variable %{THE_REQUEST}.
The value of this variable is the actual request command submitted by the requestor(Browser, bot, etc..).
Assuming your home page is named index.html the request for an index file looks alot like this:

"GET /index.html HTTP/1.1"

Example 4 - How to redirect a particular subdirectory and structure


You currently have a blog under your current domain and you want to redirect only the blog root and related
subdirectories and pages to a new domain hosted elsewhere but leave the remainder of your website unaffected.

Your site is structured like this:

http://www.jimsdomain.com/index.html
http://www.jimsdomain.com/jumsfunstuff/...
http://www.jimsdomain.com/jimsseriousstuff/...
http://www.jimsdomain.com/blog/...

The new location for the blog is:
http://www.jimsramblings.com/

Method 1

For this method, we will use the the .htaccess file in the /blog/ directory of http://www.jimsdomain.com

------------------------------------------------------------------------------------------
RewriteEngine On
RewriteRule ^(.*)$ http://www.jimsramblings.com/$1 [R=302,L]
------------------------------------------------------------------------------------------
For use in .htaccess and if it is not set globally or for the root directory of your domain, be sure to set
Options +Indexes +FollowSymLinks as needed before the RewriteEngine On directive.
Also, depending on your server configuration you may need to use RewriteBase. Typical usage is RewriteBase /
placed just after the RewriteEngine On directive. See the previous section for more on RewriteBase.

This method is exactly like the domain redirection in example 1, with the difference being where the .htaccess file
is located.


Method 2 

For this method, we will use the the .htaccess file in the root directory for http://www.jimsdomain.com

-------------------------------------------------------------------------------------------------
RewriteEngine On
RewriteRule ^/blog/(.*)$ http://www.jimsramblings.com/$1 [R=302,L]
-------------------------------------------------------------------------------------------------
For use in .htaccess and if it is not set globally or for the root directory of your domain, be sure to set
Options +Indexes +FollowSymLinks as needed before the RewriteEngine On directive.
Also, depending on your server configuration you may need to use RewriteBase. Typical usage is RewriteBase /
placed just after the RewriteEngine On directive. See the previous section for more on RewriteBase.

This method is similar to the redirection in method 1 with the difference being where the .htaccess file is located
and the RewriteRule match arguement.

The ^/blog/(.*)$ is interpreted as:

Note that the examples above are using temporary redirection (302). If the target of the redirection is gong to be
the permanent location, be sure to change the redirection to a permanent (301) once you are satisfied with the results


Example 5 - How to redirect a URL to a different port with and without conditions

Redirecting a URL and using a specific port is a question that got my head scratching one day. Someone had a login
page for example login.html. To enhance security they later decided they would set up another server that listened on
a nonstandard port (8080) and move the login page to there. To implement this they needed to employ URL and port
redirection. This is how the port redirection can be done:

--------------------------------------------------------------------------------------------------------
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[a-z]{3,9}\ /login\.html\ HTTP/ [NC]
RewriteRule ^.*login\.html$ http://secure1.example.com:8080/ [R=301,L]
--------------------------------------------------------------------------------------------------------
For use in .htaccess and if it is not set globally or for the root directory of your domain, be sure to set
Options +Indexes +FollowSymLinks as needed before the RewriteEngine On directive.
Also, depending on your server configuration you may need to use RewriteBase. Typical usage is RewriteBase /
placed just after the RewriteEngine On directive. Further details on RewriteBase are provided in a previous section.

This example is very similar to the How to redirect your home page example above except here RewriteRule and
RewriteCond match \login.html. Note that the RewriteCond insures that the target of the GET is for login.html from
only the root directory of the domain. If such a strict interpretation is not required you can remove the RewriteCond
statement. The port redirection itself is specified by the :8080 in the second argument to RewriteRule.

TIP!
You can even get more creative by modifying the RewriteCond to use HTTP_USER_AGENT in place of THE_REQUEST,
use negation on the second argument and then specify the regex for say msnbot,Slurp or Googlebot. This would cause
redirection to occur except if a search bot was requesting. This is useful because bots can't login so this would
be a method to provide crawalable content that otherwise would not get indexed.

Sounds like a good candidate for Example 6. Stay tuned!


Fixes and tips on using RewriteLog !

If RewriteLog is having a problem and not logging actions by mod_rewrite. More specifically:
Here is the fix

You need to put the RewriteLog directives in httpd.conf or the virtual host container and NOT in any directory containers
or an .htaccess file!

Also, Of course the web server has to be restarted for the new logging directives to take effect.

Example:

<VirtualHost 10.0.0.1:80>
ServerName www.example.com
........other directives

<IfModule rewrite_module>
    RewriteLog logs/rewrite.log
    RewriteLogLevel 3
</IfModule>

<Directory "/user/www">
Options Indexes FollowSymLinks +IncludesNOEXEC 
    AllowOverride FileInfo
    Order allow,deny
    Allow from all
...... other directives

    
<IfModule rewrite_module>
RewriteEngine On
... other rewrite commands
</IfModule>
 
</Directory>
 
</VirtualHost>


Now your rewrite processing should log events to the logs/rewrite.log file.
Note I used RewriteLogLevel 3 that provides a minimal amount for debugging, 9 provides the most and 0 turns it off.
Warning: Rewrite Logs get quite huge quickly (Gigabytes) and the required overhead will slow down serving of pages!
Use for testing only and preferibly not\Never on a production server!

Good Luck and best to you!


These are just some simple but useful examples demonstrating the use of mod_rewrite. Check back for new upcoming
articles on mod_rewrite!

To read more on mod_rewrite and the rewrite engine, you may try the hyperlink to the Apache documentation in the
right pane under the Related Links section.

Other methods of URL Redirection

Question: Besides the apache server side redirects, I have heard of using a meta tag in the HTML markup of
a web page for redirection. Which one should I use?

Answer:
The server side method is preferred. Webmasters should not use the meta http-equiv refresh to redirect pages as some
user agents (Browsers, Bots, etc..) may not support this function. This  technique has also been used in the past for
cloaking and other malicious reasons. If you are using meta refresh redirection and are having problems with ranking
or getting indexed by search engines, try switching to one of the redirection techniques discussed above.

That being said there may be times where you have no choice except the "meta refresh" such as if you are on a "free"
host or otherwise have limited options. In that case you if you use the meta refresh use a time delay of zero.
For example:  <meta http-equiv="refresh" content="0;url=http://www.mynewdomain.com/">

Again, all search engines may not handle this correctly but there is a great article by respected SEO guru Sebastian
that details how currently Google and Yahoo accept undelayed meta refreshs as 301 redirects. 

Basically,

Google will "consider" this as a 301 redirect as long as the timer is 0 even though no HTTP 301 response is sent.
It does this by parsing the page after it is crawled. Any time delay greater than 0 may cause unpredictable and possibly
unfavorable results or even be ignored completely.
 
Yahoo will "consider" this as a 301 redirect as long as the timer is 0 (or a "low" number), If it is a "high" number it will
consider the meta refresh a 302 temporary redirect. Unfortunatly Yahoo does not define what "Low and "High" mean.
 
One other thing I like to do for a page that uses this type of redirection is to add a normal link to the new URL
in the page body for other bots and to assist users who have the meta refresh redirection disabled in their browser.

Note: Google PageRank won't be transferred by this type of redirect even though Google considers this otherwise a 301.

For more details on the best way to create this redirected page be sure to check out Sebastians original article. 
 
Question: Are there other ways to redirect URLs besides using the apache methods or HTML META Refresh?

Answer:
Some domain registrars offer some method of domain forwarding. When considering these options be sure the redirect
is a 301 permanent redirect and not a 302 temporary. Also be sure that no cloaking or masking is involved.

While not redirection, you can alias URLs using DNS. This can be used to point a second level domain request to a third
level domains IP. i.e. example.com to www.example.com. This is a normal DNS configuration and is not considered
redirection. To avoid canonicalization issues, a form of 301 redirection would need to be done in addition.

For more DNS related information see our article on DNS Server configuration, performance and security tips. 

You can also redirect using various programming\scripting languages such as PHP, PERL, and ASP.
You can also do this with JavaScript but be warned that most search engines (and browsers with JavaScript turned off)
will not follow these redirects as this is a client side language where the others are server side. Maybe in the future -TBD.

That about does it for this version of our article on Using Apache redirection techniques.

Back to the top

If you found this page useful, consider linking to it.
Just copy (mark then ctrl-c) and paste into your website.
This is how this link will look: Using 301 redirection on Apache




Thanks!   Enjoy! I hope this is helpful!

Version 1.1   Copyright © 2007-2012 AITechSolutions.net. All rights reserved.   Terms of Use

Quick Links


Using permanent redirection

Using temporary redirection

Indicating a page is gone (410)

Using permanent redirection matching

Using temporary redirection matching

Using ServerAlias for pseudo-Redirection

Using mod_rewrite for Domain Redirection

Server 500 Error and mod_rewrite

Redirecting www and non-www URLs using mod_rewrite

Redirecting your home page using mod_rewrite

Redirecting a subdirectory using mod_rewrite

Redirecting URLs to a different port

RewriteLog not logging data

Other methods of URL Redirection


Related Links


Apache Software Documentation - URL Redirection

Apache Software Documentation - mod_rewrite

RFC on 301 redirects

Introduction to mod_rewrite tutorial