redirect http://www.site.com to http://site.com

jameso

Registered
Sep 25, 2003
4
0
151
Use the following in a php script:

PHP:
<?php 

$server = getenv("HTTP_HOST"); 

if (ereg("^www.", $server)) { 
    // Assume www. has been entered 
    Header ("Location: http://".$server."/"); 
    Exit; 
     
} 

?>
That should do what you're trying to do.

Cheers,

James
 

jameso

Registered
Sep 25, 2003
4
0
151
You can also use mod_rewrite in your .htaccess file. Put it inside your public_html directory in order for the changes to effect all files/directories.

The code below is for doing the opposite to what you wish to do (ie going from no www to a url with www). I'm not a mod_rewrite expert so I'm not sure how to reverse the effect of it:
Code:
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
Replace domain and .com as necessary.

James
 

Chew

Well-Known Member
Dec 31, 2003
96
0
156
Maryland
Thanks tons to the both of you.

Gonna have to figure out which one works best for my usage :)

Chew
 

ryno267

Well-Known Member
Mar 3, 2004
212
0
166
Chandler, AZ
cPanel Access Level
Root Administrator
Just found this and thought i'd have to butt in and ressurrect this puppy...


Redirects are ok but you should really be doing a 301 Redirect for SEM sake, and thus google will love you as will yahoo, etc.

In php:
Code:
<?php
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://new-url.com" );
?>
 

brianoz

Well-Known Member
Mar 13, 2004
1,146
7
168
Melbourne, Australia
cPanel Access Level
Root Administrator
Chew said:
I use cookies that require my visitors to use http://site.com however a large majority of the internet population uses www.site.com still.
Chew,

This doesn't make sense, because as I understand it cookies registered as being from site.com will also get sent to www.site.com and also to anything.site.com. Have you checked to see whether your cookies are getting there anyway?