Internet & Networking

Apache: How to redirect an entire server or directory to a single URL

There are two chief ways to redirect all requests for an entire server to a single location: one which requires the use of mod_rewrite, and another which uses a CGI script.

First: if all you need to do is migrate a server from one name to another, simply use the Redirect directive, as supplied by mod_alias:

Redirect /

Since Redirect will forward along the complete path, however, it may not be appropriate – for example, when the directory structure has changed after the move, and you simply want to direct people to the home page.

The best option is to use the standard Apache module mod_rewrite. If that module is compiled in, the following lines

RewriteEngine On
RewriteRule /.* [R]

will send an HTTP 302 Redirect back to the client, and no matter what they gave in the original URL, they’ll be sent to “”.

The second option is to set up a ScriptAlias pointing to a CGI script which outputs a 301 or 302 status and the location of the other server.

By using a CGI script you can intercept various requests and treat them specially, e.g., you might want to intercept POST requests, so that the client isn’t redirected to a script on the other server which expects POST information (a redirect will lose the POST information.) You might also want to use a CGI script if you don’t want to compile mod_rewrite into your server.

Here’s how to redirect all requests to a script… In the server configuration file,

ScriptAlias / /usr/local/httpd/cgi-bin/redirect_script/

and here’s a simple perl script to redirect requests:


print “Status: 302 Moved Temporarily\r\n” .
“Location:\r\n” .


Leave a Reply