You are hereRecipes for Redirection, Apache and Javascript Examples

Recipes for Redirection, Apache and Javascript Examples


By Matt - Posted on 10 May 2009

The best way to redirect a page is to use Apache's mod_rewrite.  Notoriously cryptic it is, but it's fast and not too hard to figure out for basic things.  Here's an example mod_rewrite to redirect http to https.   Put it in your apache config preferably, or your .htaccess file if you can't modify your config directly.
RewriteEngine on
RewriteCond %{SERVER_PORT} =80
RewriteRule ^(.*) https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

If you're not able to use mod_rewrite at all, here's a Javascript version of the same sort of thing:
Code:

 
<script language="JavaScript">

 
loc=top.location.href;

 
if(! loc.match(/^https/)){

 
loc=loc.replace(/^http:/,"https:");

 
top.location.href=loc;

 
}

 
</script>

Create Live Bookmark

Syndicate content

Recent comments

Latest images