WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Rewrite Rule syntax help (4 posts)

  1. milo2man
    Member
    Posted 5 years ago #

    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

    into

    http://www.example.com/blog/category/events/

    or even better

    http://www.example.com/blog/events/

    I am using the 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 :-)

  2. wpbct7
    Member
    Posted 5 years ago #

    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

  3. milo2man
    Member
    Posted 5 years ago #

    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

    Any ideas?

  4. milo2man
    Member
    Posted 5 years ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic