When designing your application infrastructure, you will probably require some form of proxy or redirect to take your client from a generic uri/host address to a default application.

For example, say you have an application that runs on https://myhost.com/myservice.
What happens when a client unintentionally requests http://myhost.com ?

Rather than return an HTTP 404 Not Found error, the more user friendly approach is to redirect the request to the secure location of your service.

Two ways you can configure apache to redirect the request:

- an index.html default response file
- an apache Rewrite rule.

index.html

For the index.html approach, the default index.html looks like this:

    # cat index.html
    <script>
    window.location="https://myhost.com/myservice/";
    </script>

Pretty simple, right ? Any request coming to the default location will be redirected to the myservice application.

Rewrite Rule
The Rewrite rule approach is a bit more robust, and can be configured to redirect any request http://myhost.com/[any path] to your secured application. The index.html only redirects requests made to the base url http://myhost.com/.

The Rewrite rule is configured by adding the following to your apache httpd.conf file:

   RewriteEngine On

   # Redirect http://myhost.com/ -> https://myhost.com/myservice/
   RewriteCond %{HTTP_HOST}        (.*) [NC]
   RewriteRule ^/(.*)$     https://myhost.com/myservice/ [L,R]

The Rewrite rule consists of a condition (HTTP_HOST == .*) - this is a generic matching condition, guaranteed to match your system's hostname.

The [NC] tag means the comparison is "Not Case sensitive"

The Rule "^/(.*)$" is applied to this Condition, decoded as follows:

Any requested uri path starting with /  (^/) containing any length path (.*)$ is redirected to https:/myhost.com/myservice/

[L,R] means when this condition is met, stop processing the Rule [L] and issue the Redirect [R].

I think the Rewrite rule gives you a bit more flexibility than the index.html approach, but I suggest you give them both a try.

Reference - a good one page Rewrite Rule cheat sheet can be downloaded at:

http://www.addedbytes.com/apache/mod_rewrite-cheat-sheet/

Comments:

You can also perform index.html redirection like this:
$ cat index.html
<meta HTTP-EQUIV="REFRESH" content="0; url=https://myhost.com/myservice/">

Posted by therek on October 18, 2008 at 09:00 AM EDT #

You're making it harder (and more inefficient) than it needs to be.
Insted of serving a page or starting mod_rewrite which is quite heavy, all you really need is a Redirect or a RedirectMatch in your httpd.conf

http://httpd.apache.org/docs/2.2/mod/mod_alias.html#redirect

http://httpd.apache.org/docs/2.2/mod/mod_alias.html#redirectmatch

Posted by mads on October 21, 2008 at 07:23 PM EDT #

Thanks, Jay - I needed to redirect my base URL but not the sub-URLs underneath it, and the index.html option worked perfectly.

Posted by gareth on February 10, 2009 at 07:36 PM EST #

That was Really helpful, the index.html redirect is excatly what I was looking for. Thanks Jay.

Posted by Stuart on February 24, 2009 at 09:51 AM EST #

Post a Comment:
  • HTML Syntax: NOT allowed

This blog copyright 2009 by Jay Danielsen