The ultimate site redirect

Add the following lines to your .htaccess file:

RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

of course, you would change example.com to your domain name, and example\.com to your domain name with backslashes before the dots.

Add the following JavaScript to a file that is common to your whole website.

try {
    if (window.top.location.href != document.location.href) {
        window.top.location = document.location;
    }
} catch (ex) {
    var alllinks = document.getElementsByTagName("a");
    for (var i=0; i<alllinks.length; i++) {
        alllinks[i].setAttribute("target", "_top");
    }
}

Here’s how it works:

The .htaccess file tells your server to redirect all domains except your domain that are pointed at your files to change to your domain.

The JavaScript tells browsers to redirect out of frames, and if that is not possible, to change all links so they will do so.  This prevents users of browsers like FireFox 3 from not going directly to your website.

One thought on “The ultimate site redirect

Comments are closed.