So I was pretty sure that my htaccess redirect rule was perfect but it simply won’t work no matter what when tested from davidtan.org (main domain of a shared host).
Here’s what I added into .htaccess
RewriteCond %{HTTP_HOST} ^(www\.)?davidtan\.org$ [NC] # for main domain on a shared host
RewriteRule ^(certain-string.+)/$ http://newdomain.com/$1/ [R=307,L] # 301, 302, 307 – choose one
What this basically does is redirect ONLY certain pages on davidtan.org with url matching a pattern “certain-string.+” to a new domain, which will then bear the same url.
After some testing, I’ve finally found out what the main problem was. It was to do with cache.
Google Chrome Caches Redirects
Google Chrome is my main web browser where I do most of my browsing and web development these days. However, one of the caveats when testing redirects in Google Chrome is that you have to remember that Google Chrome caches redirects. Yes, you read that right.
So for example, if I made an error in my redirect rule that causes an infinite loop, Google Chrome caches that results. The problem persists even after you’ve corrected your rewrite rule. The only way is to restart and clear the cache of Google Chrome.
Caching Plugins
I run this blog using WordPress together with the W3 Total Cache plugin to speed up loading time. What I didn’t realize was that this plugin does browser caches too. I will have to clear all caches to make sure that my redirects are actually executed whenever there’s a change in my htaccess redirect rules.
Conclusion
I guess the lesson you can learn here is that be aware of how caches can affect your redirects. It can become really frustrating testing redirect rules that looks absolutely correct but not getting the right results just because your browser or caching plugin cached a bad redirect while you were doing your testing.
Leave a Reply