Using mod rewrite to redirect someone based on their search phrase

Blogging is really fun and can provide people with lots of useful information. When you’ve had a blog for as many years as I have, you see trends evolve. My blog is mainly about myeloma, but a lot of people go there to read about shingles.

People find the blog by searching on a variety of phrases, but they may find just one of my many pages about the experiences I’ve had with shingles. What if I want them to land on my main shingles post that has a link to all the other pages? This bit of code from my .htaccess file ensures that folks who searched on “shingles” alone or in addition to “picture,” “treatment” or “rash,” will be sent to the main blog post about shingles.

RewriteCond %{HTTP_REFERER} [?+&]?shingles[?+&]? [NC]
RewriteCond %{HTTP_REFERER} [?+&]?(picture|treatment|rash?)[?+&]? [NC]
RewriteCond %{REQUEST_URI} !^alternate_page\.html$
RewriteCond %{REQUEST_URI} !^([^/]+/)*[^/.]+\.[^/.]+$
RewriteRule .*  [R=302,L]

You must be sure that RewriteEngine On is included and that your server (Apache) is allowing mod rewrite. I have no idea how this would be accomplished using IIS, since I don’t use or care for IIS at all. I’ve been using Apache since 1995 (on linux or some *nix), and will stick with what I know best (and what works best!).

Keep in mind that other rewrites put in place by programs such as WordPress might interfere with your own code. If you end up with errors, try another way.