Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

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:
    166
    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:
    8
    Trophy Points:
    193
    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,051
    Likes Received:
    2
    Trophy Points:
    168
    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.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
Loading...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice