Support » Fixing WordPress » Tag 301 redirects using .htaccess

  • Resolved bradlby


    Is it possible to use a .htaccess file to redirect /index.php?tag=example to /tag/example/

    I have a ton of tags so I’d like to write a rule that reads the query_string as opposed to writing each one out. I’ve tried just about everything I can think of RewriteCond and RewriteRule-wise, but I can’t seem to any results. Even if I do write out a specific case nothing happens, so I must be missing some key element here.

Viewing 8 replies - 1 through 8 (of 8 total)
  • whooami



    and what rewrite rules have you tried?

    I’m definitely no expert, but initially I tried

    RewriteCond %{QUERY_STRING} ^tag=$1
    RewriteRule ^/index.php$1 ^/tag/$1 [R=301,L]

    This to me seems like the most simplistic solution, everything beyond that was just tinkering (removing the ^, changing $1 to just $, etc)




    RewriteRule ^tag/(.+)/?$ /index.php?tag=$1 [QSA,L]

    try that. thats untested.

    actually oops:

    RewriteRule ^tag/([^/]*)/?$ /index.php?tag=$1 [QSA,L]




    and Ill admit I dont know what you mean by “reads the query_string as opposed to writing each one out.

    that kinda confused me, so the above might not be what youre after.

    meaning I know what the query string is, but Im not understanding if youre just trying to prettify those, or something else.

    Basically there are 2 URLs that are generated by WordPress when you tag a post.

    So I want to consolidate those by redirecting all index.php?tag=example pages to its corresponding /tag/example/ directory.

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    I would have thought that if you have the pretty permalinks turned on, WordPress 2.3 will do that by itself, as part of the canonical permalinks stuff. Apparently not.

    Might want to submit a bug report about this one on trac.


    RewriteCond %{QUERY_STRING} ^tag=(.*)$
    RewriteRule .* /tag/%1 [R,L]

    Patterns in RewriteRules don’t match query-strings. You have to match them in RewriteCond’s and use their values in back references later (with % marks instead of $ marks).

    I must still be missing something here. I tried:

    Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{QUERY_STRING} ^tag=(.*)$
    RewriteRule .* /tag/%1 [R,L]

    But I still don’t get anything redirecting. Do you think it’s related to the RewriteBase? I am pretty much useless when it comes to the syntax of mod_rewrite, so I’m probably overlooking some small but key detail.

    Oho my friends and compatriots, I have the final answer for you:

    RewriteCond %{QUERY_STRING} ^tag=(.*)$
    RewriteRule .* /tag/%1/? [R=301,L]

    If you leave the ? off the end you’ll end up with an infinite rewrite because it will try to make /tag/example/?tag=example. Thanks for your help everyone!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Tag 301 redirects using .htaccess’ is closed to new replies.