Moved Blog Delivers AdSense PSAs, What to Do?

Someone wrote to me today and said “Help, I just moved my WordPress blog over to another server. My blog address is http://www.example.com/blog, and I just found out that if people omit the “www”, and just type “http://example.com/blog” they still get to my blog, but all of my AdSense ads are public service announcements (which AdSense uses if they don’t know about your page, and you don’t earn anything from them). When people type the normal full address, with the “www”, the AdSense ads display perfectly. What can I do about this?!”

Well, as it happens, there is something you can do about this, and it’s actually pretty easy.

What you need to do is create an Apache Rewrite rule.

What? You say that doesn’t sound easy? I know, but it is.

Basically what the rewrite functon of Apache does is fake out the viewer’s browser by taking it to the URL you specify, rather than the URL the viewer typed in. In this case, when a user types “http://example.com/blog” it will take them to “http://www.example.com/blog”. Now, virtually it is the same thing on one level, however AdSense knows about your content which resides at “http://www.example.com/blog” but does not know about content which purports to reside at “http://example.com/blog”. By redirecting the browser to “http://www.example.com/blog”, the AdSense fairies are happy and deliver the right ads for your content.

Here is the recipe to work this magic. Put this in a file called “.htaccess” (without the quotation marks, but with the leading period):

RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain\.com
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=permanent,L]

If you already have a file called .htaccess, you may already have the “RewriteEngine on” in there; if so, omit it from the above recipe, you only need it in there once.

If this does not work, check with your ISP to make sure that they have Apache’s RewriteEngine set to “On”, and if not, ask them to please make that adjustment for you.