WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Tag 301 redirects using .htaccess (9 posts)

  1. bradlby
    Member
    Posted 6 years ago #

    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.

  2. whooami
    Member
    Posted 6 years ago #

    and what rewrite rules have you tried?

  3. bradlby
    Member
    Posted 6 years ago #

    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)

  4. whooami
    Member
    Posted 6 years ago #

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

    try that. thats untested.

    actually oops:

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

  5. whooami
    Member
    Posted 6 years ago #

    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.

  6. bradlby
    Member
    Posted 6 years ago #

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

    example.com/index.php?tag=example
    and
    example.com/tag/example/

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

  7. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    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.

    Also:

    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).

  8. bradlby
    Member
    Posted 6 years ago #

    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.

  9. bradlby
    Member
    Posted 6 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic