A Business Technology Place

Using httpd.conf to redirect index.html

The problem
Our default landing page was a jsp file located on the application server (not the web server).  The default page set by the web server required a local file so we left it as index.html. Then inside the index.html we embedded a meta refresh command to forward visitors to the jsp page.

So if you typed a URL as www.webserver.com it would auto forward to www.webserver.com/LandingPage.jsp.

This created two problems:
1. It’s a security issue to use the meta refresh technique in a html file.
2. The http referrer information is lost for analytics data on the jsp page. That creates analysis challenges because the referring site and links of the visitors on your site is lost. You can’t tell which site they were linking from or if they came via a search engine.

Potentials Solutions
Setup the redirect in the .htaccess file or in the httpd.conf file on the webserver instead of the index.html file. The difference is that the .htaccess file is interpreted by the webserver on each transaction whereas directives in the httpd.conf are compiled and stored in server cache.  So the directives in the httpd.conf should be more efficient but require a server administrator to restart the webserver with each change.

The webserver we were working with is a high transaction webserver so efficiency was a big concern.  We chose to use the httpd.conf file for our directives.

The rules and syntax
The rules and syntax for directives in a httpd.conf file are a challenge. I found information in this article useful. Our requirement was to direct URL requests to our server root to the jsp page so we setup the following conditions and directive:

rewriteCond %{HTTP_HOST} ^www\.webserver\.com$
rewriteCond %{REQUEST_URI} ^/$
rewriteRule ^/?$ https://www.webserver.com/LandingPage.jsp [R=301,L]

The first two lines set the condition that must be true for the redirect to trigger. The host must be the name of your site and the URI must be a slash (or the root). Remember the visitor does not explicitly key index.html, it’s implied because it is the default file set for the web container instance.  If the condition is true then page will redirect to our landing page jsp file.

The trickiest piece was the first half of the rewriteRule.
^ is the begin of the string match
/ means the slash character
? means match 0 or 1 times
$ is the end of the string match

We ended up putting the ‘?’ character because it wasn’t apparent to us if the ‘/’ was added by the web server or the browser by default. In some instances we noticed the ‘/’ in the html title.

Also, we needed the R=301 to specify a http 301 (permanent) redirect to both maintain the http referrer for analytics data as well as tell search engine robots that this is a permanent landing page.