Published by Mijingo

tips icon image

EE Insider Tips
Sponsored by Mijingo's EE 2 Screencasts

ExpressionEngine mini-howtos created by the EE Insider community.

Force WWW or no-WWW

User's avatar
  • Posted by Philip Zaengle
  • May 29, 2009
  • 6 people have found this tip helpful.

I don’t care which, but this’ll help you a lot when working with logged in vrs. logged out users.

If your not on the same domain (http://www.eeinsider.com vrs http://eeinsider.com) you can end up with all sort funky things happening (mainly cookies being set in the wrong place, and other such confusion). This of course is also good from an SEO stand as to not have duplicate content, but this is not an SEO tip.

You can do this using .htaccess
EDIT: The following code has been updated to allow for the destination to remain the same when switch to the forced domain. - June 1, 2009

<IfModule mod_rewrite.c>
  
RewriteEngine On

  
# force www  
  
RewriteCond %{HTTP_HOST} !^www\.
  
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [R=301,L] 

  # remove www
  #RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  #RewriteRule ^(.*)$ http://%1/ [R=301,L]
  
</IfModule


Old Code - Force the www.

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

Remove the www.

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

further reading:
link: how to force www or no www in domain name
link: www or no www seo question

OrganizedFellow19:32 on 05.30.2009

This seems to work fine, except, when I go to:

http://example.com/index.php/site/
all is good, as expected.

But if I enter:
http://www.example.com/index.php/site/
I am sent to
http://www.example.com/

Philip Zaengle04:11 on 06.01.2009

Hey OrganizedFellow, good catch. I’ve updated the post to allow for those sorts of issues. hope that helps!

Add Your Comment?

You must have an EE Insider account to post comments on Insider Tips. It's fast, easy and hassle-free.

Sign up now (or login).

Search EE Insider Tips
Sponsored By