Home : Linux : Servers : Apache :

Redirect

mod_rewrite

This module provides a rule-based rewriting engine to rewrite requested URLs on the fly. Apache documentation

A simple example is our basic Redirect and equivalent RedirectMatch examples

   Redirect / http://anotherdomain.tld/

   RedirectMatch ^/(.*) http://anotherdomain.tld/$1

   RewriteEngine on RewriteRule ^/(.*) http://anotherdomain.tld/$1 [R,L?]

All do the same thing. Each tool has an increasing level of options. Redirecting is just one aspect of ModRewrite. ModRewrite will do anything you couldn't do with RedirectMatch, and a whole lot more.

The Apache 1.3 URL Rewriting Guide says mod_rewrite:

...provides a powerful way to do URL manipulations. With it you can nearly do all types of URL manipulations you ever dreamed about.

A number of interesting examples are also provided.
[ comment | link | top ]

Redirect example

The basic/simple examples are elsewhere on this page. This one was a pretty unique situation and probably isn't very useful to others.

I have a number of sub-domains/hosts whose document roots are sub-directories of the domains document root. Because they are sub-domains - host.domain.com, not domain.com/host - their links are relative to their document root - /domain/host, not /domain. While you can get to http:domain.com/host/ and it may work, following any links - http:domain.com/host/page.html - won't. I also have a simple file tree search on domain that is unaware of the various hosts. For clarity the host directories are all upper case, a minimum of 3 letters. Using RedirectMatch or mod_rewrite I can insure that all domain.com/host URL's get routed to host.domain.com.

   RedirectMatch ^/([A-Z]{3}[^/]*)/(.*) http://$1.domain.com/$2

The match must start (^) with a slash followed by three capital letters ([A-Z]{3}). If there's anything else before the second slash ([^/]*) it is included in the match. This allows for directory names longer than three capitol letters, e.g. FLEX32. Everything between the first and second slash is the host name/document root/sub-directory/ and we store it ([A-Z]{3}[^/]*) as variable $1. Everything after the slash is a host sub-directory or file and may or may not exist so we store anything or nothing (.*) as variable $2. RedirectMatch will send http://domain.com/HOST/ to http://host.domain.com/ and http://domain.com/HOST/foo/bar.html to http://host.domain.com/foo/bar.html

...for mod_rewrite ( [R] can be included but is repetitious and not required)

   RewriteEngine on
   RewriteRule ^/([A-Z]{3}[^/]*)/(.*) http://$1.domain.com/$2
[ comment | link | top ]

Redirect

The Redirect directive maps an old URL into a new one. Apache documentation

Redirect can map a single page to a single page or map an entire domain to a different domain. The syntax is the same for internal (same domain) and external (anotherdomain) redirects.

The Redirect default is is labeled temporary (302). Redirect options include:

   RedirectPermanent/Redirect permanent/Redirect 301
   RedirectTemp/Redirect temp/Redirect 302
   Redirect gone/Redirect 410

See also RedirectMatch (below).

Examples:

   Redirect / http://anotherdomain.tld/

will map http://thisdomain.tld/any/dir/or/page.html to http://anotherdomain.tld/any/dir/or/page.html. Anything after the / (http://thisdomain.tld/) will be appended to http://anotherdomain.tld/.

   Redirect /foo/bar.html http://anotherdomain.tld/baz.html

will map http://thisdomain.tld/foo/bar.html to http://anotherdomain.tld/baz.html
[ comment | link | top ]

RedirectMatch

This directive is equivalent to Redirect, but makes use of standard regular expressions, instead of simple prefix matching. Apache documentation.

The RedirectMatch equivalent of the basic

   Redirect / http://anotherdomain.tld/

is

   RedirecMatch ^/(.*) http://anotherdomain.tld/$1

With the ability to use standard regular expressions you can solve most of the redirection needs that couldn't be done with the simpler Redirect. If you need even more options take a look at mod_rewrite.
[ comment | link | top ]

Back to: Apache