Website http://www.domain.com; index.php - 301 redirect domain.com to www.domain.com

Discussion in 'PHP' started by northpointaiki, Jan 5, 2006.

  1. #1
    Hello all - I earlier posted something on this, but was probably not clear enough with what I am looking for. I have looked in many places, but have not found how to redirect all "http://domain.com" to "http://www.domain.com", if the site is php based.

    This site is an add-on domain to my other site. Using C-Panel doesn't seem possible, as I can't see anything set up re: the add-on domain within the redirect tools.

    I did do a redirect for my main domain (thanks, Minstrel), after mucking about with the .htaccess Options in the _vti_bin folders (I have FP on my main site, and needed to alter the options for FP to work with the redirect in .htaccess).

    Now, the add-on domain has the following:

    The .htaccess file in the public_html only contains the following:

    DirectoryIndex index.php


    The index.php file begins with:

    <?php
    require_once '_db_interface.php';
    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>
    <head>

    Can someone please provide some advice, given that index is a .php file?

    Thanks.

    Paul
     
    northpointaiki, Jan 5, 2006 IP
  2. TMan

    TMan Peon

    Messages:
    126
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You'll have to check if you have to redirect (http://domain.com) before you sent any output to the browser. I suppose there is a server/environment variable for this, check php.net. (maybe $_SERVER['HTTP_HOST'] is a good one)

    After this, you have to use the header() function to sent the redirect to the browser.
     
    TMan, Jan 5, 2006 IP
  3. northpointaiki

    northpointaiki Guest

    Messages:
    6,876
    Likes Received:
    187
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks TMan -

    Very much a noob here. Can you please explain further?
     
    northpointaiki, Jan 5, 2006 IP
  4. northpointaiki

    northpointaiki Guest

    Messages:
    6,876
    Likes Received:
    187
    Best Answers:
    0
    Trophy Points:
    0
    #4
    TMan, and others - I tried various header schemes, all of which returned errors. Being an asbolute noob, I don't really know what you are speaking of when you're talking about:

    But I am guessing what you are saying is that I have to put in the above server variable as a first line in index.php, followed by the header redirect, in order for the header redirect to work, correct? (maybe this is why I kept getting the errors...?)

    Nevertheless, I tried this:

    Inside root .htaccess: after

    DirectoryIndex index.php

    I added:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www
    RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [L,R=301]


    - and the redirect seems to be working. Is there any reason (security or otherwise) this wouldn't be a preferred method? Anyone?

    Thanks.
     
    northpointaiki, Jan 6, 2006 IP
  5. hasitruparel

    hasitruparel Peon

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    does anyone knows how to do this in windows 2003 with asp? i want respective page to go to respective page, like domainname.com/yahoo.html to www.domainname.com/yahoo.html
     
    hasitruparel, May 12, 2006 IP