Saturday, November 4th, 2006

How do you redirect a webpage from http://domain.com to http://www.domain.com without mod_rewrite?

This weird and annoying problem I’ve spent quite some time on to solve today. The problem occurred on one of the websites that I administrate. The webshop on the site refused to go through with purchases from users accessing the website from domain.com, but worked fine for users accessing it from www.domain.com.

Don’t get me started on why the host doesn’t support mod_rewrite, but that was something that I didn’t have any power over, so I had to come up with some workaround.

If mod_rewrite had been supported, the problem would have been easy to solve. Then all I would have to do would have been to add the following lines to the .htaccess-file (I tried this and ended up with a 500-error):

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [R,L]

So how did I solve this?
The solution is really ugly, and I’m not very proud of it, but it works for now. What I did was that I created an index.html-file (which didn’t exist, since the site is using PHP) like the one below:

Example-file (since the blog is restricted, and I cannot write html here). Right-click and save it. If you left-click on it, you will be redirected (duh!).

The next thing I did was a simple .htaccess-hack. Since my server loaded index.php before index.html, I added the following line to .htaccess:

DirectoryIndex index.html index.php

(Apache looks for the files in the order they’re listed)

This took care of the entire problem. I never said it was a nice solution, au contraire, it’s really ugly, but it gets the job done.

If you have any suggestions on how to improve/solve this differently, please let me know.

Update 1: We describe a method to do this for a server over which you have full control in this article.


3 Responses to “How do you redirect a webpage from http://domain.com to http://www.domain.com without mod_rewrite?”

  1. Viktor Petersson Says:

    My friend Ben wrote me an e-mail informing me about another possible workaround. Here it goes:

    “You *might* be able to use (before anything else on the page)
    if ($_SERVER[’SERVER_NAME’] != www.domain.tld) {
    header(”Location: http://www.domain.tld/path/name.ext“);
    }

    The caveat is that $_SERVER[’SERVER_NAME’] may return www.domain.tld
    even though the requested URI was domain.tld. A possible workaround
    is given here: http://www.apacheref.com/ref/http_core/
    UseCanonicalName.html. But this assumes you have privileges to
    configure apache.”

    It might work, I don’t know, but since I’m running a complete CMS-system, I’d try to avoid hacking the files too much (to make it easier when updating etc.). But thanks Ben, it might be a better solution for some of our readers.

  2. Alexander Ljungberg Says:

    The CMS e107 has functionality like this built in. On the preferences page you can enter the primary host name of the website and then there is an option to redirect all users to that name.

    So for http://www.norwinter.com for example, some users come in through http://ext2.norwinter.com. due to old links and stuff. They all end up getting www.norwinter.com in their address bar.

  3. Viktor Petersson Says:

    I might also add that another solution that Alex suggested was to use the Redirect-feature in .htaccess. I found something that was called RedirectMatch. The command is given as follows:

    “RedirectMatch (.*).gif$ http://www.anotherserver.com$1.jpg

    The benefit to this in contrast to regular Redirect is that you can use regular-expression to match. However, what I ended up with was an infinite loop, since I redirected over and over to the same page.

    This might work for other people though, but not for me.