View Full Version : PHP redirect .domain to www.domain
hasen
Oct 27th 2007, 11:41 am
A common question but I only know how to do it in htaccess. I've been informed that my server is running in PHP Suexec mode so I can't use htaccess to redirect domain.com to www.domain.com. I know I need to create a php.ini file in my public_html directory but I'm not sure of the code. I had a lot of trouble finding anything about it in Google (almost all hits were about how to do it in htaccess) and anything I did find didn't appear to work.
Thanks in advance.
litebulb1
Oct 27th 2007, 12:13 pm
I believe that you only have to change the header. check out this website:
http://us3.php.net/header
Fash
Oct 27th 2007, 12:40 pm
if(strpos($_SERVER['HTTP_HOST'], "www.") !== 0){
header("Location: http://www." . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
}
That ought to do it :)
Might not be the best idea to let them specify the HOST they want to go to, so if you want, you can do it like this as well:
$site = "mysite.com";
if(strpos($_SERVER['HTTP_HOST'], "www.") !== 0){
header("Location: http://www." . $site . $_SERVER['REQUEST_URI']);
}
hasen
Oct 27th 2007, 1:12 pm
So I don't need the php.ini file? Someone at cpanel support told me I need a php.ini file in the public_html folder.
tonybogs
Oct 27th 2007, 4:26 pm
If you have direct access to apache its better to take care of this sort of thing there.
Create an alias and have it redirect to www...
This sort o f thing doesnt really belong inside the application in my opinion (but hey thats just me ;) )
Hope this helps
hasen
Oct 28th 2007, 3:39 am
Yes I have direct access to apache but I'm not quite sure what you mean by creating an alias there...
bLuefrogx
Oct 28th 2007, 4:54 am
$site = "mysite.com";
if(strpos($_SERVER['HTTP_HOST'], "www.") !== 0){
header("Location: http://www." . $site . $_SERVER['REQUEST_URI']);
}
Dump that in an include file and remember to include it on every one of your pages ;)
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.