Apache Redirect

I recently ran into an expression in an httpd.conf file that got me to researching Redirect

The configuration directives tell Apache to get content from a specific place in the filesystem and return it to the client. Sometimes it is desirable, instead to inform the client that the requested content is located at a different URL, and instruct the client to make a new request with the new URL. This is called redirection and is implemented by the Redirect directive. For example, if the contents of the directory /foo under the DocumentRoot are moved to /bar, you can instruct clients to request the content at the new location as follows:

Redirect permanent /foo http://www.example.com/bar

This will redirect any URL-path starting in /foo to the same URL path on the http://www.example.com server with /bar substituted for /foo. You can redirect clients to any server, not only to the origin server.

Apache also provides a RedirectMatch directive for more complicated rewriting problems.  For example, to redirect requests for the site home page to a different site, but leave all other requests alone, use the following:

RedirectMatch permanent ^/$ http://www.example.com/startpage.html

Alternatively, to temporarily redirect all pages on one site to a particular page on another site, use the following:

RedirectMatch temp .* http://othersite.example.com/startpage.html

I also notice an interesting character in one of my Redirect statements

RedirectMatch (?i)\/foo http://www.example.com/bar/

The RedirectMatch uses regular expressions. The (?i) stands for case insensitive so the above expression would match /foo /Foo /FOo /FOO /fOO etc.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s