I currently have a .htaccess file that looks like this:

RewriteCond %{QUERY_STRING} ^action=view&item=([0-9]+)$
RewriteRule ^index\.php$ /index.php?url=item/%1 [R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

It is meant to 301 redirect my old query string based URLs to new CakePHP URLs. This will successfully send users to the correct page. However, Google doesn't seem to like it (see below). I previously tried doing this:

RewriteCond %{QUERY_STRING} ^action=view&item=([0-9]+)$
RewriteRule ^index\.php$ /item/%1 [R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

But that fails. The second rewrite rule doesn't seem to catch the rewritten URL. It goes straight through.

Using the first version wouldn't be a problem, except that I suspect that is what is choking up Google. It hasn't indexed my sitemap full of the new URLs. My old sitemap had been fully indexed and all the URLs are in Google's index. But it isn't following the redirects from the old URLs to the new. I have a 'not followed' error for every one of the query URLs that was in my old sitemap.

Am I properly using a 301 redirect here? Is it the weird rewrite rule? What can I do to send both Google and users to the proper page and save my page rank?

  • Dan this is perfect for StackOverflow.com – Frank Nov 13 '10 at 8:54
  • I thought this was perfect for Webmasters. Especially considering how many other similar questions have been asked. Well, if I'm not going to get an answer here I'll ask there. – Daniel Bingham Nov 14 '10 at 3:54
  • Have you tried checking the server headers for the redirect? There are various free tools online where you can do this. It may turn out you're not sending the right header for redirection. – DisgruntledGoat Nov 15 '10 at 1:15

Quoting from the mod_rewrite documentation:

"You will almost always want to use [R] in conjunction with [L] (that is, use [R,L]) because on its own, the [R] flag prepends http://thishost[:thisport] to the URI, but then passes this on to the next rule in the ruleset, which can often result in 'Invalid URI in request' warnings."

In fact, I'd recommend using [NS,L,R=code] for external redirects, unless you have a particular reason to rewrite subrequests too.

Ps. This is what Google has to say about "Not Followed" errors. It looks like a malformed redirect is indeed a likely cause of them. Besides adding the missing [L] flag, I'd suggest using something like Firebug's Net panel to see exactly what your server is sending back in the Location header.


What can I do to send both Google and users to the proper page and save my PageRank?

Just to be certain: you're not redirecting Google and users differently, are you?

Sorry if I'm mistaken about this, as I don't know regex but it looks like you're rewriting the URL twice? Is that correct?

RewriteRule ^index\.php$ /item/%1 [R=301]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

Wouldn't that be considered a redirect loop?

  • It would be, if it weren't for those RewriteConds that prevent the second rule from applying to any URLs that resolve to an existing file (like index.php). – Ilmari Karonen Mar 13 '12 at 1:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.