Need to force the "www" on my WP site.

22 replies
I'm trying to figure out how to make sure all visitors to the site always end up at www(dot)(thedomain)(dot)com.

I've set the Wordpress URL and Site URL to both include the www.

I've set the preferred site to the www version in Google's webmaster tools.

Some of my inbound links have www, but most don't.

Some of the links without www will still take the user to the URL with www in it. While others will leave it without. I have no clue why this is, the links themselves definitely point to the non-www version.

Typing in the URL without the www will not forward to the www version.

I tried inserting redirect code I found into my .htaccess and it broke the site (removed it right away of course). I'm using W3 Total Cache which puts a lot of stuff in the .htaccess file. Maybe that's why it broke?

How do I get this sealed up?

EDIT:
Adding all the useful details up here in the first post.

To summarize the behavior:
  • Typing in "domain.com" results in "http://domain.com/"
  • "domain.com/" results in "http://domain.com/"
  • "domain.com/path" results in "http://www.domain.com/path/"
  • "domain.com/path/" results in "http://domain.com/path/"
  • At least one link pointing to http://domain.com results in "http://www.domain.com/"
  • Most or all other links pointing to the same, result in "http://domain.com/"

Also, I guess there is no point in obfuscating the domain. It's my blog, in my signature, if you'd like to see how it behaves yourself.

I'm using the W3 Total Cache plug-in for WordPress. This has some of it's own mod_rewrite stuff in the .htaccess file. Here are the sections of my .htaccess file which have mod_rewrite stuff.

Code:
# BEGIN W3TC Page Cache core
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^(.*/)?w3tc_rewrite_test$ $1?w3tc_rewrite_test=1 [L]
    RewriteCond %{HTTP_USER_AGENT} (2.0  mmp|240x320|alcatel|amoi|asus|au-mic|audiovox|avantgo|benq|bird|blackberry|blazer|cdm|cellphone|danger|ddipocket|doc$
    RewriteRule .* - [E=W3TC_UA:_low]
    RewriteCond %{HTTP_USER_AGENT} (acer  s100|android|archos5|blackberry9500|blackberry9530|blackberry9550|cupcake|docomo  ht-03a|dream|htc hero|htc magic|$
    RewriteRule .* - [E=W3TC_UA:_high]
    RewriteCond %{HTTP_COOKIE} w3tc_referrer=.*(google.com|yahoo.com|bing.com|ask.com|msn.com) [NC]
    RewriteRule .* - [E=W3TC_REF:_search_engines]
    RewriteCond %{HTTPS} =on
    RewriteRule .* - [E=W3TC_SSL:_ssl]
    RewriteCond %{SERVER_PORT} =443
    RewriteRule .* - [E=W3TC_SSL:_ssl]
    RewriteCond %{HTTP:Accept-Encoding} gzip
    RewriteRule .* - [E=W3TC_ENC:.gzip]
    RewriteCond %{REQUEST_METHOD} !=POST
    RewriteCond %{QUERY_STRING} =""
    RewriteCond %{REQUEST_URI} /$ [OR]
    RewriteCond %{REQUEST_URI} (sitemap.xml(.gz)?) [NC]
    RewriteCond %{REQUEST_URI} !(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|index.php) [NC,OR]
    RewriteCond %{REQUEST_URI} (wp-comments-popup.php|wp-links-opml.php|wp-locations.php) [NC]
    RewriteCond %{HTTP_COOKIE} !(comment_author|wp-postpass|wordpress_[a-f0-9]+|wordpress_logged_in) [NC]
    RewriteCond  "%{DOCUMENT_ROOT}/wp-content/w3tc/pgcache/%{REQUEST_URI}/_index%{ENV:W3TC_UA}%{ENV:W3TC_REF}%{ENV:W3TC_SSL}.html%{ENV:W3TC_ENC}"  -f
    RewriteRule .*  "/wp-content/w3tc/pgcache/%{REQUEST_URI}/_index%{ENV:W3TC_UA}%{ENV:W3TC_REF}%{ENV:W3TC_SSL}.html%{ENV:W3TC_ENC}"  [L]
</IfModule>
# END W3TC Page Cache core
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{http_host} ^domain.com[NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
#force #site #www
  • Profile picture of the author webapex
    Some hosting control panels have easier to use .www settings.

    Here's one more article on .httaccess, your probably already close, maybe some rearranging of code lines...
    Signature

    “An expert is a person who has made all the mistakes that can be made in a very narrow field” Niels Bohr

    {{ DiscussionBoard.errors[3988212].message }}
  • Profile picture of the author MervikHaums
    Hey,

    I didn't rewrite .htaccess. I simply made the site url as
    Code:
    http://www.mysiteurl.com
    and all the pages even if its not www prefixed is taking to the www version of the url.

    I made the change here - /wp-admin/options-general.php
    Signature
    => Ecommerce? Shopify?
    FB Ads? Check This 3 Minutes Video!
    <=
    {{ DiscussionBoard.errors[3988220].message }}
    • Profile picture of the author Kevin Kimes
      Originally Posted by webapex View Post

      Some hosting control panels have easier to use .www settings.

      Here's one more article on .httaccess, your probably already close, maybe some rearranging of code lines...
      Thanks. I'll have to try to wrap my head around that tomorrow, if nobody has a quickie solution.

      Originally Posted by MervikHaums View Post

      Hey,

      I didn't rewrite .htaccess. I simply made the site url as
      Code:
      http://www.mysiteurl.com
      and all the pages even if its not www prefixed is taking to the www version of the url.

      I made the change here - /wp-admin/options-general.php
      Thanks, but I've done that already, as I tried to mention . It does work for all URLS except the home page URL. Any posts, pages, etc, all work and show the www even if I type in the URL without it. Just the home page doesn't, and I want to fix that.
      Signature
      Google Hates Cheap Content! Fill your sites with content people actually want to read, before it's too late.
      I write engaging & interesting content.


      My blog: Online Business Lifestyle
      -Kevin Kimes
      {{ DiscussionBoard.errors[3988236].message }}
      • Profile picture of the author Daws0n
        Originally Posted by Kevin Kimes View Post

        Thanks. I'll have to try to wrap my head around that tomorrow, if nobody has a quickie solution.


        Thanks, but I've done that already, as I tried to mention . It does work for all URLS except the home page URL. Any posts, pages, etc, all work and show the www even if I type in the URL without it. Just the home page doesn't, and I want to fix that.
        that's quite weird :confused: because if all posts, pages are redirecting to www version even if you type a non www url then your homepage should also be redirected unless a line of code in your settings or in .htaccess file is not allowing it. i am not that good with .htaccess thing and that's why i contacted my host to take care of canonical stuff for me by paying them a small fee :p
        Signature
        I don't have a signature dude!
        {{ DiscussionBoard.errors[3988256].message }}
        • Profile picture of the author Kevin Kimes
          Originally Posted by Daws0n View Post

          that's quite weird :confused: because if all posts, pages are redirecting to www version even if you type a non www url then your homepage should also be redirected unless a line of code in your settings or in .htaccess file is not allowing it. i am not that good with .htaccess thing and that's why i contacted my host to take care of canonical stuff for me by paying them a small fee :p
          Your post prompted me to do some checking.

          If I type the URL in without a trailing slash, it will always redirect to the www version. If I include the trailing slash, it will not. Except for the home page, which will not redirect either way.
          Signature
          Google Hates Cheap Content! Fill your sites with content people actually want to read, before it's too late.
          I write engaging & interesting content.


          My blog: Online Business Lifestyle
          -Kevin Kimes
          {{ DiscussionBoard.errors[3988292].message }}
  • Profile picture of the author uniqueseo
    ya this is necessary if you are not using this then make 301 redirect because this is beneficial for SEO.
    {{ DiscussionBoard.errors[3988397].message }}
    • Profile picture of the author Kevin Kimes
      Originally Posted by uniqueseo View Post

      ya this is necessary if you are not using this then make 301 redirect because this is beneficial for SEO.
      Yes, but how?
      Signature
      Google Hates Cheap Content! Fill your sites with content people actually want to read, before it's too late.
      I write engaging & interesting content.


      My blog: Online Business Lifestyle
      -Kevin Kimes
      {{ DiscussionBoard.errors[3988459].message }}
  • Profile picture of the author Raitei_Ericko
    Is there any advantage for using www?
    {{ DiscussionBoard.errors[3988547].message }}
    • Profile picture of the author UMS
      Originally Posted by Raitei_Ericko View Post

      Is there any advantage for using www?
      There's no advantage or disadvantage. The main thing is to be consistent and to make sure you have a redirect in place to go to the www or non-www domain.
      {{ DiscussionBoard.errors[3988554].message }}
      • Profile picture of the author theimdude
        Originally Posted by UMS View Post

        There's no advantage or disadvantage. The main thing is to be consistent and to make sure you have a redirect in place to go to the www or non-www domain.
        Yes and No. The other day I changed DNS to a ecommerce site of mine and on the one machine I logged on with http : //www . domainname . com and the other http : // domainname.com

        The one was on the old ip and the other on the new ip. Messed up my orders.

        Had to wait +- 2 days for site to work properly
        Signature
        Do you want 30 back-links in my PRIVATE BLOG network for ONLY $20 ???
        [LIMITED ACCESS + FREE ARTICLE INCLUDED OR YOUR OWN]

        CLICK HERE NOW
        {{ DiscussionBoard.errors[3992531].message }}
      • Originally Posted by UMS View Post

        There's no advantage or disadvantage. The main thing is to be consistent and to make sure you have a redirect in place to go to the www or non-www domain.
        While technically true, the important thing is the mindset of those who will link back to your site. People expect a www in the same way they are used to a .com

        So if you look at who is most likely to link back to you it could sway your decision one way or the other. I choose to use www because the majority of links back to my site are www. If yours are not then I would go that direction.
        {{ DiscussionBoard.errors[3999001].message }}
  • Profile picture of the author StoneWilson
    I met the same problem before, then after checked on Google, I found the way, Edit the htaccess file on ftp:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    rewriteEngine on
    rewriteCond %{http_host} ^example.com[NC]
    rewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>

    # END WordPress

    It works on my websites, maybe you should try.
    Signature
    Looking for godaddy renewal coupon? Check GodaddyRenewal.com!
    {{ DiscussionBoard.errors[3988553].message }}
    • Profile picture of the author Kevin Kimes
      Originally Posted by StoneWilson View Post

      I met the same problem before, then after checked on Google, I found the way, Edit the htaccess file on ftp:

      # BEGIN WordPress
      <IfModule mod_rewrite.c>
      rewriteEngine on
      rewriteCond %{http_host} ^example.com[NC]
      rewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
      RewriteEngine On
      RewriteBase /
      RewriteRule ^index.php$ - [L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . /index.php [L]
      </IfModule>

      # END WordPress

      It works on my websites, maybe you should try.
      I changed mine to match that, and there was no change to the behavior.

      To summarize the behavior:
      • Typing in "domain.com" results in "http://domain.com/"
      • "domain.com/" results in "http://domain.com/"
      • "domain.com/path" results in "http://www.domain.com/path/"
      • "domain.com/path/" results in "http://domain.com/path/"
      • At least one link pointing to http://domain.com results in "http://www.domain.com/"
      • Most or all other links pointing to the same, result in "http://domain.com/"

      Also, I guess there is no point in obfuscating the domain. It's my blog, in my signature, if you'd like to see how it behaves yourself.

      I'm using the W3 Total Cache plug-in for WordPress. This has some of it's own mod_rewrite stuff in the .htaccess file. Here are the sections of my .htaccess file which have mod_rewrite stuff.

      Code:
      # BEGIN W3TC Page Cache core
      <IfModule mod_rewrite.c>
          RewriteEngine On
          RewriteBase /
          RewriteRule ^(.*/)?w3tc_rewrite_test$ $1?w3tc_rewrite_test=1 [L]
          RewriteCond %{HTTP_USER_AGENT} (2.0 mmp|240x320|alcatel|amoi|asus|au-mic|audiovox|avantgo|benq|bird|blackberry|blazer|cdm|cellphone|danger|ddipocket|doc$
          RewriteRule .* - [E=W3TC_UA:_low]
          RewriteCond %{HTTP_USER_AGENT} (acer s100|android|archos5|blackberry9500|blackberry9530|blackberry9550|cupcake|docomo ht-03a|dream|htc hero|htc magic|$
          RewriteRule .* - [E=W3TC_UA:_high]
          RewriteCond %{HTTP_COOKIE} w3tc_referrer=.*(google.com|yahoo.com|bing.com|ask.com|msn.com) [NC]
          RewriteRule .* - [E=W3TC_REF:_search_engines]
          RewriteCond %{HTTPS} =on
          RewriteRule .* - [E=W3TC_SSL:_ssl]
          RewriteCond %{SERVER_PORT} =443
          RewriteRule .* - [E=W3TC_SSL:_ssl]
          RewriteCond %{HTTP:Accept-Encoding} gzip
          RewriteRule .* - [E=W3TC_ENC:.gzip]
          RewriteCond %{REQUEST_METHOD} !=POST
          RewriteCond %{QUERY_STRING} =""
          RewriteCond %{REQUEST_URI} /$ [OR]
          RewriteCond %{REQUEST_URI} (sitemap.xml(.gz)?) [NC]
          RewriteCond %{REQUEST_URI} !(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|index.php) [NC,OR]
          RewriteCond %{REQUEST_URI} (wp-comments-popup.php|wp-links-opml.php|wp-locations.php) [NC]
          RewriteCond %{HTTP_COOKIE} !(comment_author|wp-postpass|wordpress_[a-f0-9]+|wordpress_logged_in) [NC]
          RewriteCond "%{DOCUMENT_ROOT}/wp-content/w3tc/pgcache/%{REQUEST_URI}/_index%{ENV:W3TC_UA}%{ENV:W3TC_REF}%{ENV:W3TC_SSL}.html%{ENV:W3TC_ENC}" -f
          RewriteRule .* "/wp-content/w3tc/pgcache/%{REQUEST_URI}/_index%{ENV:W3TC_UA}%{ENV:W3TC_REF}%{ENV:W3TC_SSL}.html%{ENV:W3TC_ENC}" [L]
      </IfModule>
      # END W3TC Page Cache core
      # BEGIN WordPress
      <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %{http_host} ^domain.com[NC]
      RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
      RewriteEngine On
      RewriteBase /
      RewriteRule ^index.php$ - [L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . /index.php [L]
      </IfModule>
      Signature
      Google Hates Cheap Content! Fill your sites with content people actually want to read, before it's too late.
      I write engaging & interesting content.


      My blog: Online Business Lifestyle
      -Kevin Kimes
      {{ DiscussionBoard.errors[3992407].message }}
      • Profile picture of the author theimdude
        This is what I normally do with cpanel

        Signature
        Do you want 30 back-links in my PRIVATE BLOG network for ONLY $20 ???
        [LIMITED ACCESS + FREE ARTICLE INCLUDED OR YOUR OWN]

        CLICK HERE NOW
        {{ DiscussionBoard.errors[3992511].message }}
        • Profile picture of the author Kevin Kimes
          Originally Posted by theimdude View Post

          This is what I normally do with cpanel

          I tried both "with or without" and "Do Not Redirect www.". Both had no effect at all. They both inserted rewrite rules at the end of the .htaccess file. The site continued working as before, with inconsistent redirects. I made sure browser cache wasn't affecting the tests.
          Signature
          Google Hates Cheap Content! Fill your sites with content people actually want to read, before it's too late.
          I write engaging & interesting content.


          My blog: Online Business Lifestyle
          -Kevin Kimes
          {{ DiscussionBoard.errors[3998995].message }}
  • Profile picture of the author SteveJohnson
    You have a couple of errors in your rewrite...
    Environment variables are usually case-sensitive on Apache servers. Use
    Code:
    RewriteCond %{HTTP_HOST} ...
    instead of lower-case http_host.

    In the pattern match, you have to escape the '.':
    Code:
    RewriteCond %{HTTP_HOST} ^domain\.com
    Also, it might work better if you were to use the negative incantation:
    Code:
    
    RewriteCond %{HTTP_HOST} !^www\.$
    RewriteRule ^(.*)$ http://www.your_domain.com/$1 [R=301]
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[3992664].message }}
    • Profile picture of the author Kevin Kimes
      This:
      Code:
      # END W3TC Page Cache core
      # BEGIN WordPress
      <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %{HTTP_HOST} !^www.$
      RewriteRule ^(.*)$ http://www.kevinkimes.com/$1 [R=301]
      RewriteEngine On
      RewriteBase /
      RewriteRule ^index.php$ - [L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . /index.php [L]
      </IfModule>
      broke the site. It would not load anything at any URL.

      This:
      Code:
      # END W3TC Page Cache core
      # BEGIN WordPress
      <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %{HTTP_HOST} ^kevinkimes.com[NC]
      RewriteRule ^(.*)$ http://www.kevinkimes.com/$1 [R=301]
      RewriteEngine On
      RewriteBase /
      RewriteRule ^index.php$ - [L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . /index.php [L]
      </IfModule>
      did not un-break it.

      This:
      Code:
      # END W3TC Page Cache core
      # BEGIN WordPress
      <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteBase /
      RewriteRule ^index.php$ - [L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . /index.php [L]
      </IfModule>
      un-broke it. But I'm still back to where I was with the inconsistent redirects.


      So, it seems, inserting the correct code into that area broke the site for some reason, where my incorrect code (which obviously did nothing) didn't break the site.
      Signature
      Google Hates Cheap Content! Fill your sites with content people actually want to read, before it's too late.
      I write engaging & interesting content.


      My blog: Online Business Lifestyle
      -Kevin Kimes
      {{ DiscussionBoard.errors[3998962].message }}
  • Profile picture of the author Ephrils
    Some Hosting panels can fix this automatically for you.

    If you've done that and it's still not fixed use htaccess. It's powerful so be careful, but it will also solve your problem.
    Signature

    Two Signature lines for rent.

    {{ DiscussionBoard.errors[3999439].message }}
  • Profile picture of the author Terry Crim
    There is an internal setting in Wordpress Settings where you can choose WWW or non www or both. Wordpress I believe DOES NOT use htaccess for this feature BUT if you use htaccess to do that DO NOT use the internal wordpress setting or you will cause an internal loop that will stop your page from loading.

    I had that issue earlier when I was messing with the samething you are. I suggest just let wordpress do it.

    - T
    {{ DiscussionBoard.errors[3999802].message }}
  • Profile picture of the author BobbyTn
    was this an addon domain or the main domain? if you dont add the www. when youre adding the domina name no matter how you configure the WP you may end up with the results your discribing I had that... I deleted the domain and re-add it with www.
    hope this helps
    Signature

    Engage Your Website Visitors Using the RIGHT Video Player Plugin! [URL]https://mvp.wpqualityplugins.com//https://mvpplugin.com/URL]

    {{ DiscussionBoard.errors[4000332].message }}
    • Profile picture of the author Kevin Kimes
      Originally Posted by BobbyTn View Post

      was this an addon domain or the main domain? if you dont add the www. when youre adding the domina name no matter how you configure the WP you may end up with the results your discribing I had that... I deleted the domain and re-add it with www.
      hope this helps
      My cpanel installation does not allow typing in www. before the addon domains. When trying, it will tell me that it automatically handles both, and makes me do it over without the www. in front.
      Signature
      Google Hates Cheap Content! Fill your sites with content people actually want to read, before it's too late.
      I write engaging & interesting content.


      My blog: Online Business Lifestyle
      -Kevin Kimes
      {{ DiscussionBoard.errors[4000526].message }}

Trending Topics