The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Auto-redirecting to www.domain

Discussion in 'General Discussion' started by DReade83, Jul 1, 2007.

  1. DReade83

    DReade83 Well-Known Member

    Joined:
    Oct 20, 2006
    Messages:
    196
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Cheshire, UK
    How can I automatically redirect users to www.mydomain.com who visit mydomain.com (without the www)?

    I could use PHP code:
    PHP:
    if(!ereg("www"$_SERVER['HTTP_HOST'])) {
       
    header("Location: http://www.mydomain.com/"); exit();
    }
    But this is a little fiddly. Is there an easier was of doing it?
     
  2. Spiral

    Spiral BANNED

    Joined:
    Jun 24, 2005
    Messages:
    2,023
    Likes Received:
    7
    Trophy Points:
    0
    Instead of using PHP code in an index script, you can just simply add
    mod_rewrite commands to your .htaccess to re-write urls to use "www" ...
     
  3. madaboutlinux

    madaboutlinux Well-Known Member

    Joined:
    Jan 24, 2005
    Messages:
    1,052
    Likes Received:
    2
    Trophy Points:
    38
    Location:
    Earth
    Yes, you should write rewrite rules in .htaccess instead of a php code. Paste the below code in your .htaccess file

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

    replace 'domain.com' the actual domain name.
     
Loading...

Share This Page