7

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: show@412customs.com, 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?

Jeff Atwood
  • 63,320
  • 48
  • 150
  • 153
ntgCleaner
  • 5,865
  • 9
  • 48
  • 86
  • 1
    What web server are you running? – Mike Caron Jul 02 '11 at 00:11
  • You probably have to set up the DNS records correctly. – Felix Kling Jul 02 '11 at 00:13
  • That's a great question Mike, I am hosted on a Linux server with 1and1.com, I will have to call and find out what server they are using. I guess I don't know enough about that part of the web! I will call and find out – ntgCleaner Jul 02 '11 at 00:18
  • 1
    btw, a lot of hosting companies will let you set either www or the plain domain as the canonical domain (and have the other one redirect) through the web control panel. I don't know about 1and1, but this is a very common setting. – joelhardi Jul 02 '11 at 00:25

4 Answers4

4

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/

patorjk
  • 2,164
  • 1
  • 20
  • 30
  • 2
    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. – Kai Qing Jul 02 '11 at 00:14
  • 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! – ntgCleaner Jul 02 '11 at 00:17
  • 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 ;) – Richard Pianka Jul 02 '11 at 00:17
  • 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 Jul 02 '11 at 00:28
  • I'm going to try the php setcookie('ver_ame', $email, time()+2592000, "/", ".taskconductor.com"); – ntgCleaner Jul 02 '11 at 00:36
  • 1
    @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. – Kai Qing Jul 02 '11 at 00:59
  • FYI Kai, here's a [better way](http://stackoverflow.com/questions/88011/make-apache-automatically-strip-off-the-www) 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. – joelhardi Jul 02 '11 at 01:18
  • 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. – Richard Pianka Jul 02 '11 at 04:19
  • 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. – Kai Qing Jul 07 '11 at 00:32
4

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!

ntgCleaner
  • 5,865
  • 9
  • 48
  • 86
2

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.

Richard Pianka
  • 3,317
  • 2
  • 28
  • 36
  • 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 Jul 02 '11 at 00:22
  • sorry, Just read up - setcookie('ver_ame', $email, time()+2592000, "/", ".taskconductor.com"); should work – ntgCleaner Jul 02 '11 at 00:36
1

If you explicitly set your cookie domain to taskconductor.com (no www), then the same single set of cookies will be used for both the www and the naked domain. You'll just need to modify your PHP to specify a cookie domain.

I would recommend you do as others are suggesting and do a redirect to whichever version you want to use as the canonical URL. It's bad practice to have duplicate content across multiple (sub) domains. But, it's also a good idea to understand the domain scope of cookies that you set.

joelhardi
  • 11,039
  • 3
  • 32
  • 38
  • 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 Jul 02 '11 at 00:36
  • Yes, that should fix it. [setcookie()](http://php.net/manual/en/function.setcookie.php) 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 Jul 02 '11 at 01:00
  • This is perfectly fine, the site isn't in use yet besides testers. Thank you! – ntgCleaner Jul 02 '11 at 01:51