WWW and non-WWW URL. Two different sites

10,829

Solution 1

I was able to set my php "setcookies" to have a specified domain.

My original setcookie string was: setcookie('ver_ame', $email, time()+2592000);

This only allowed the cookie to be set on whatever type of page it was on. If it were on http: //taskconductor.com it would set the cookie for that, and also the same if it were http: //www.taskconductor.com.

If your setcookie string is: setcookie('ver_ame', $email, time()+2592000, "/", ".taskconductor.com");

The additional "/" shows the cookie to work on any of the directories under the root. The ".taskconductor.com" part would be showing which domain to use. The fact that it has a period before the web name shows that this cookie will work on any subdomain or its own domain.

Thank you all for the responses and help! It all works now! THANK YOU!

Solution 2

Do you know what web server you are using? If you're using apache, you can rewrite the URL in the .htaccess file. This will allow you to funnel all your traffic to with your non-www domain. I did a quick google and found this sample code:

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

Source: http://yoast.com/how-to-remove-www-from-your-url-with-mod_rewrite/

Solution 3

Better than using URL rewrites is to set your cookies to work for subdomains. For example, if you set the cookie for mydomain.com, then it will not work for sub.mydomain.com. However, if you set the cookie for .mydomain.com (notice the period), then it will work for mydomain.com, sub.mydomain.com, foobar.mydomain.com etc.

Share:
10,829
ntgCleaner
Author by

ntgCleaner

Hello World

Updated on June 12, 2022

Comments

  • ntgCleaner
    ntgCleaner almost 2 years

    I just noticed today that a website I am creating has the WWW or non-WWW problem. If you go to http: //www.taskconductor.com, it is a different page with the same content as just http: //taskconductor.com.

    If you were to login (username: [email protected], Pass: tester) at http: //www.taskconductor.com, then try to go to http: //taskconductor.com (without the WWW), it will make you log in again. Then as you can see, when you check your cookies, you can see that there are two sets of cookies. One for http: //taskconductor.com and one for http: //www.taskconductor.com.

    I have seen that this is a problem, but do I need to make a redirect? and if so, does it have to be index.php? I would really prefer to have all of my main content on index.php.

    How can I get around this?

  • Kai Qing
    Kai Qing almost 13 years
    Pretty much exactly what I was going to suggest. Search engines will also read www and non www as separate sites, so you should always make the choice to either include or exclude the www like the htaccess example above.
  • ntgCleaner
    ntgCleaner almost 13 years
    I did see this same code when I was searching for this stuff. I didn't understand what it was all saying, but I was in the middle of work. I will try to edit the htaccess and let you know how it goes. Thank you!
  • Richard Pianka
    Richard Pianka almost 13 years
    This is a poor work-around for this problem, and any search engine that considers a www subdomain as a different site literally isn't worth using. Try Google ;)
  • ntgCleaner
    ntgCleaner almost 13 years
    wow, That's great to know! Thank you. I am setting my cookies with php though. so I'm not able to specify the domain. It just happens on whatever page it's using
  • ntgCleaner
    ntgCleaner almost 13 years
    I just changed the htaccess file to use the code above, though, to no avail. At this point, I don't care too much about google's searches, I only care if someone wants to type in the full URL...
  • ntgCleaner
    ntgCleaner almost 13 years
    So, I believe going from "setcookie('ver_ame', $email, time()+2592000);" to "setcookie('ver_ame', $email, time()+2592000, "/", ".taskconductor.com");" should change the problem?
  • ntgCleaner
    ntgCleaner almost 13 years
    sorry, Just read up - setcookie('ver_ame', $email, time()+2592000, "/", ".taskconductor.com"); should work
  • ntgCleaner
    ntgCleaner almost 13 years
    I'm going to try the php setcookie('ver_ame', $email, time()+2592000, "/", ".taskconductor.com");
  • Kai Qing
    Kai Qing almost 13 years
    @Richard - Care to explain why this is a bad work around? Almost all major sites force www or non www. Maybe not for this very purpose, but none the less, it is very common. And suggesting using a real search engine is comical, and all developers wish we could force the good ones on the users, but obviously its beyond our control and not really helpful on a site like this. I'm not necessarily being a jerk here. I really do want to know if there is a reason to not do this via htaccess.
  • joelhardi
    joelhardi almost 13 years
    Yes, that should fix it. setcookie() reference agrees with using / and .example.com as the parameters for domain-wide cookies. Note that this will not make any old cookies set for www.example.com work on example.com, it will only make all cookies from now on use example.com and work on any (sub)domain.
  • joelhardi
    joelhardi almost 13 years
    FYI Kai, here's a better way to do the redirect than mod_rewrite. If you have control over the global Apache config, it's also a good idea to disable .htaccess as it's slow (full file tree scan for these files on every request!). I agree 100% with you on the main issue though, that it is good practice not to duplicate content across domains -- URLs are meant to be canonical resource locations (see RFCs) and Google etc.'s webmaster guidelines warn against mirroring content like this.
  • ntgCleaner
    ntgCleaner almost 13 years
    This is perfectly fine, the site isn't in use yet besides testers. Thank you!
  • Richard Pianka
    Richard Pianka almost 13 years
    It's not a bad idea in general, it's just not the solution to this problem. And good point, I was being kind of snarky before--apologies.
  • Kai Qing
    Kai Qing almost 13 years
    Yeah, full scan per request is pretty weak, but if one has no access to global apache config then it's at least a viable option. Good to know.