Ok, I’m getting a bit closer. I found this here.
Redirecting a working URI to a new format
Here’s a curly one. Let’s say, for example, that we’ve got a set of working URLs that look like this: /index.php?id=nnnn. However, we’d really like to change them to /nnnn and make sure search engines update their indexes to the new URI format. First, we’d have to redirect the old URIs to the new ones so that search engines update their indexes, but we’d still have to rewrite the new URI back to the old one so that the index.php script would run. Have I got your head spinning?
The trick here is to place into the query string a marker code that will not be seen by visitors. We redirect from the old link to the new format only if the “marker” is not present in the query string. Then we rewrite the new format link back to the old format, and add a marker to the query string, using the QSA flag to ensure we’re not eliminating an existing query string. Here’s how it’s done:
RewriteCond %{QUERY_STRING} !marker
RewriteCond %{QUERY_STRING} id=([-a-zA-Z0-9_+]+)
RewriteRule ^/?index\.php$ %1? [R=301,L]
RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?marker&id=$1 [L]
Here, the original URI, http://www.example.com/index.php?id=nnnn
does not contain the marker, so it’s redirected by the first rule to http://www.example.com/nnnn with a HTTP 301 response. The second rule rewrites http://www.example.com/nnnn back to http://www.example.com/index.php?marker&id=nnnn, adding marker and id=nnnn in a new query string; then, the mod_rewrite process is started over.
In the second iteration, the marker is matched so the first rule is ignored and, since there’s a dot character in index.php?marker&id=nnnn, the second rule is also ignored … and we’re finished!
So, following this I changed my .htaccess to include:
RewriteEngine On
RewriteBase /blog/
RewriteCond %{QUERY_STRING} !marker
RewriteCond %{QUERY_STRING} cattag=([-a-zA-Z0-9_+]+)
RewriteRule ^/?category/blog/$ %1/? [R=301,L]
RewriteRule ^/?([-a-zA-Z0-9_+]+)$ /category/blog/?marker&cattag=$1 [L]
The first rule does succeed in redirecting
http://www.example.com/blog/category/blog/?cattag=events
to
http://www.example.com/blog/events/
in the browser, but it returns a 404 error.
I can’t seem to make the second rule rewrite the pretty link back to the original format for WP to find the resource. There is no . in my URL like in the example, so it is probably not being ignored as it shoudl be???
I must be making a simple syntax error as I tried to adapt it from the poster’s original code. Can anyone help me get the second rewrite rule correct?