Title: Rewrite Rule syntax help
Last modified: August 19, 2016

---

# Rewrite Rule syntax help

 *  Resolved [milo2man](https://wordpress.org/support/users/milo2man/)
 * (@milo2man)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/rewrite-rule-syntax-help/)
 * Can anyone help me with the syntax for what is I hope a simple rewrite rule? 
   I am a relative newbie and haven’t been able to get it to work despite all the
   posts on permalinks/rewrites.
 * I want to turn
 * [http://www.example.com/blog/category/blog/?cattag=events](http://www.example.com/blog/category/blog/?cattag=events)
 * into
 * [http://www.example.com/blog/category/events/](http://www.example.com/blog/category/events/)
 * or even better
 * [http://www.example.com/blog/events/](http://www.example.com/blog/events/)
 * I am using the [TDO Tag Fixes ](http://wordpress.org/extend/plugins/tdo-tag-fixes/)
   plugin which allows me to show all posts with the tag ‘events’ within the category‘
   blog’. This is so I can exclude posts from other categories with the tag ‘events’.
 * My permalinks are set to /%postname%/ with a category base of ‘category’ and 
   tag base of ‘tag’.
 * My .htaccess is currently
 *     ```
       RewriteEngine On
       RewriteBase /blog/
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteCond %{REQUEST_FILENAME} !-d
       RewriteRule . /blog/index.php [L]
       ```
   
 * I don’t want to mess with the WP structure at this point because I’ve got the
   site up and running after much effort and everything seems to work. I would just
   like to get rid of the ugly link string 🙂

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [wpbct7](https://wordpress.org/support/users/wpbct7/)
 * (@wpbct7)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/rewrite-rule-syntax-help/#post-903734)
 * Your rewrite rule should look like:
 *     ```
       RewriteEngine On
       RewriteBase /blog/
   
       RewriteRule category/blog/\?cattag=events events/ [L]
   
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteCond %{REQUEST_FILENAME} !-d
       RewriteRule . /blog/index.php [L]
       ```
   
 * You can find more examples here:
    [http://httpd.apache.org/docs/2.0/rewrite/rewrite_guide.html](http://httpd.apache.org/docs/2.0/rewrite/rewrite_guide.html)
 *  Thread Starter [milo2man](https://wordpress.org/support/users/milo2man/)
 * (@milo2man)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/rewrite-rule-syntax-help/#post-903893)
 * Thanks for this tip. I did exactly as you suggested, but the link still comes
   up as:
 * [http://www.example.com/blog/category/blog/?cattag=events](http://www.example.com/blog/category/blog/?cattag=events)
 * Any ideas?
 *  Thread Starter [milo2man](https://wordpress.org/support/users/milo2man/)
 * (@milo2man)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/rewrite-rule-syntax-help/#post-903897)
 * Ok, I’m getting a bit closer. I found this [here](http://www.sitepoint.com/article/apache-mod_rewrite-examples/2/).
 * > 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](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](http://www.example.com/nnnn)
   > with a HTTP 301 response. The second rule rewrites [http://www.example.com/nnnn](http://www.example.com/nnnn)
   > back to [http://www.example.com/index.php?marker&id=nnnn](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](http://www.example.com/blog/category/blog/?cattag=events)
 * to
 * [http://www.example.com/blog/events/](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?

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Rewrite Rule syntax help’ is closed to new replies.

## Tags

 * [mod_rewrite](https://wordpress.org/support/topic-tag/mod_rewrite/)
 * [permalinks](https://wordpress.org/support/topic-tag/permalinks/)
 * [rewrite rule](https://wordpress.org/support/topic-tag/rewrite-rule/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [milo2man](https://wordpress.org/support/users/milo2man/)
 * Last activity: [17 years, 5 months ago](https://wordpress.org/support/topic/rewrite-rule-syntax-help/#post-903897)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
