7 Replies - 1680 Views - Last Post: 21 July 2010 - 02:22 AM

#1 CodeVillain  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 143
  • Joined: 10-July 10

How to make my site always load as "www"

Posted 20 July 2010 - 03:45 PM

How can I force my site to always load "www" at the beginning when someone tries to go to it?

So we're clear, I mean when someone tries to go to "http://mysite.com" it'll automatically load/redirect to "http://www.mysite.com"

I use cPanel for hosting if it's host specific, I just can't seem to easily find any info on this online.
Is This A Good Question/Topic? 0
  • +

Replies To: How to make my site always load as "www"

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How to make my site always load as "www"

Posted 20 July 2010 - 03:46 PM

I'll ask my first question then see what you say: Why does it matter if they arrive at http://www.mysite.com or http://mysite.com?
Was This Post Helpful? 0
  • +
  • -

#3 CodeVillain  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 143
  • Joined: 10-July 10

Re: How to make my site always load as "www"

Posted 20 July 2010 - 04:15 PM

View PostPsychoCoder, on 20 July 2010 - 01:46 PM, said:

I'll ask my first question then see what you say: Why does it matter if they arrive at http://www.mysite.com or http://mysite.com?

Because I'm having login errors if they arrive http://mysite.com they login and for some reason I cannot figure out when they login at http://mysite.com and it redirects them to what should be the member login index page at http://www.mysite.com they're still logged out until they login again. However, if they start on http://www.mysite.com and they login then there's no issues.

I've tried to figure out how to fix the problem but I don't see anything wrong with my code, the only thing I can think is that it's reading it sort of as two different sites so the session variables aren't transferring over. (kinda like how Google Analytics requires you specify)

Also, if I did want to put analytics on it it would be easier to just have the www instead of having to look at how both performed.
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: How to make my site always load as "www"

Posted 20 July 2010 - 04:36 PM

I'm going to take a guess on your login problems. I'm betting it's the following.

- Your login system relies on cookies.
- Your cookies are set on the www subdomain specifically
- So requests from mysite.com don't get a hold of the cookies because the browser won't retrieve for a specific subdomain

I don't recommend differentiating between www subdomain and lack of subdomain. Accept both, prefer the mysite.com form (and redirect appropriately).

It's not a good idea to differentiate between www.mysite.com and mysite.com if there's no difference except the name. It makes analysis (as well as other things) harder.
Was This Post Helpful? 1
  • +
  • -

#5 CodeVillain  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 143
  • Joined: 10-July 10

Re: How to make my site always load as "www"

Posted 20 July 2010 - 04:46 PM

View PostOler1s, on 20 July 2010 - 02:36 PM, said:

I'm going to take a guess on your login problems. I'm betting it's the following.

- Your login system relies on cookies.
- Your cookies are set on the www subdomain specifically
- So requests from mysite.com don't get a hold of the cookies because the browser won't retrieve for a specific subdomain

I don't recommend differentiating between www subdomain and lack of subdomain. Accept both, prefer the mysite.com form (and redirect appropriately).

It's not a good idea to differentiate between www.mysite.com and mysite.com if there's no difference except the name. It makes analysis (as well as other things) harder.


I appreciate the information as well as the suggestions. For the moment I just specifically redirected each page, which isn't going to be practical as the site grows since using cPanel's 301 redirect I have to manually put in each page.

I'll look into fixing the cookies, although at the moment I don't know how since I'm still a complete amateur when it comes to PHP.
Was This Post Helpful? 0
  • +
  • -

#6 KingCuddles  Icon User is offline

  • D.I.C Regular

Reputation: 176
  • View blog
  • Posts: 496
  • Joined: 20-December 08

Re: How to make my site always load as "www"

Posted 20 July 2010 - 05:30 PM

View PostPsychoCoder, on 20 July 2010 - 09:46 PM, said:

I'll ask my first question then see what you say: Why does it matter if they arrive at http://www.mysite.com or http://mysite.com?


It matters in a big way

  • first it presents a cleaner cooperate image
  • second, as already points out it can help with cookie sub domain errors
  • and last, and most importantly it stops canonical URL issues


At OP: I was having a similar issues, and the method for setting domain wide sessions is discussed in depth in my thread here

Of course if you want to redirect not www. users to www. anyway you could add the following code to your htaccess file..

RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^example.co.uk [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]
Was This Post Helpful? 0
  • +
  • -

#7 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5310
  • View blog
  • Posts: 27,210
  • Joined: 10-May 07

Re: How to make my site always load as "www"

Posted 20 July 2010 - 06:50 PM

View PostPsychoCoder, on 20 July 2010 - 04:46 PM, said:

Why does it matter if they arrive at http://www.mysite.com or http://mysite.com?

It's fine configuring Apache vhosts if you only have a couple of domains, but anymore than 20 & it's a pain in the ass to manage now 40 vhosts configs. *that's one for each*
Was This Post Helpful? 1
  • +
  • -

#8 RyanRobinson  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 227
  • Joined: 31-March 10

Re: How to make my site always load as "www"

Posted 21 July 2010 - 02:22 AM

You can get the URL to redirect to your www. domain by editing your websites .htaccess file which is located at the root of your website.

Browse to your website directory using cPanel's file manager and edit the .htaccess file as follows:

Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^yoursite.com [NC] 
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301] 



Just change "yoursite.com" to your domain.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1