Forwarding to the same url without the www

Discussion in 'Site & Server Administration' started by MattEvers, Apr 5, 2006.

  1. #1
    How do I do like slashdot does when you type in www.slashdot.com and it goes to slashdot.com? The easiest way if there is more then one.

    Thanks in advance.
     
    MattEvers, Apr 5, 2006 IP
  2. TheHoff

    TheHoff Peon

    Messages:
    1,530
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if ( !preg_match("/^example.com/i",$_SERVER['HTTP_HOST']) ) { 	header("HTTP/1.1 301 Moved Permanently"); 
    	if (strlen($_SERVER['QUERY_STRING'])>0) { header("location:http://example.com".$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']); }	
    	else if (stristr($_SERVER['PHP_SELF'],"index")) { header("location:http://example.com/"); }
    	else { header("location:http://example.com".$_SERVER['PHP_SELF']); } exit;
    }
    PHP:
    This way takes care of query strings and forwards the user to the correct page. It also forwards to / as the root document instead of "index".
     
    TheHoff, Apr 5, 2006 IP
    MattEvers likes this.