PHP redirect .domain to www.domain

Discussion in 'PHP' started by hasen, Oct 27, 2007.

  1. #1
    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.
     
    hasen, Oct 27, 2007 IP
  2. litebulb1

    litebulb1 Peon

    Messages:
    151
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    litebulb1, Oct 27, 2007 IP
  3. Fash

    Fash Peon

    Messages:
    37
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    if(strpos($_SERVER['HTTP_HOST'], "www.") !== 0){
    	header("Location: http://www." . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
    }
    PHP:
    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']);
    }
    PHP:
     
    Fash, Oct 27, 2007 IP
  4. hasen

    hasen Peon

    Messages:
    994
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    hasen, Oct 27, 2007 IP
  5. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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
     
    tonybogs, Oct 27, 2007 IP
  6. hasen

    hasen Peon

    Messages:
    994
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yes I have direct access to apache but I'm not quite sure what you mean by creating an alias there...
     
    hasen, Oct 28, 2007 IP
  7. bLuefrogx

    bLuefrogx Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    $site = "mysite.com";
    if(strpos($_SERVER['HTTP_HOST'], "www.") !== 0){
        header("Location: http://www." . $site . $_SERVER['REQUEST_URI']);
    }
    PHP:
    Dump that in an include file and remember to include it on every one of your pages ;)
     
    bLuefrogx, Oct 28, 2007 IP