Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite a - to a + as conditional?

mod_rewrite a - to a + as conditional?

         

adambeazley

11:29 pm on Jan 25, 2008 (gmt 0)

10+ Year Member



Hey everyone,

I am trying to redirect traffic from an old wordpress blog to a new EE blog. Ive gotten all of the posts and categories redirecting, but I am having trouble with the tag links.

I am trying to accomplish all of the following redirects with one mod_rewrite:


oldwordpressite.com/blog/index.php?tag=keyword --> newsite.com/blog/tags/tag/keyword

oldwordpressite.com/blog/index.php?tag=two-keywords --> newsite.com/blog/tags/tag/two+keywords

oldwordpressite.com/blog/index.php?tag=long-tail-keyword --> newsite.com/blog/tags/tag/long+tail+keyword

The problem i am running into is the keywords are separated by dashes in wordpress and they are separated by plus signs in EE. Whats the best way to do this?

This is what i have so far, but it doest replace the - with a +


RewriteRule ^blog/index.php?tag=([A-Za-z0-9-]+)$ http://www.example.com/index.php/blog/tags/tag/$1 [R=301,L]

[edited by: jdMorgan at 12:16 am (utc) on Jan. 26, 2008]
[edit reason] example.com [/edit]

jdMorgan

12:38 am on Jan 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The easiest way to do it is case-by-case:

# Nine keywords
RewriteCond %{QUERY_STRING} ^tag=([^\-]+)-([^\-]+)-([^\-]+)-([^\-]+)-([^\-]+)-([^\-]+)-([^\-]+)-([^\-]+)-([^\-]+)$
RewriteRule ^blog/index\.php$ http://www.example.com/index.php/blog/tags/tag/%1+%2+%3+%4+%5+%6+%7+%8+%9? [R=301,L]
#
# Eight keywords
RewriteCond %{QUERY_STRING} ^tag=([^\-]+)-([^\-]+)-([^\-]+)-([^\-]+)-([^\-]+)-([^\-]+)-([^\-]+)-([^\-]+)$
RewriteRule ^blog/index\.php$ http://www.example.com/index.php/blog/tags/tag/%1+%2+%3+%4+%5+%6+%7+%8? [R=301,L]
#
... Rules for seven through three keywords
#
# Two keywords
RewriteCond %{QUERY_STRING} ^tag=([^\-]+)-([^\-]+)$
RewriteRule ^blog/index\.php$ http://www.example.com/index.php/blog/tags/tag/%1+%2? [R=301,L]
# One keyword
RewriteCond %{QUERY_STRING} ^tag=([^\-]+)$
RewriteRule ^blog/index\.php$ http://www.example.com/index.php/blog/tags/tag/%1? [R=301,L]

Problems:
  • There is a limit of nine back-references per pattern in RewriteRule and RewriteCond. So any one rule can handle a maximum of nine keywords.
  • The "+" character must be hex-encoded if used in a URL (as opposed to a query string). This makes for very ugly URLs.
  • There is no way to "loop" a small section of mod_rewrite code -- You have to restart it from the top.
  • Looping the whole .htaccess file is very slow (See [N] flag on RewriteRule).
  • Looping often triggers a nasty mod_rewrite bug [archive.apache.org] which remains unpatched to this day.
  • Using sequential "stacked" or "chained" rules can trigger the same bug.
  • The solution for the bug is really ugly.

    If you can meet your requirements using nine rules like the example rules above, then you'll likely be happier doing it that way. If not, things are going to get ugly -- and/or very slow.

    Jim

  • adambeazley

    1:04 am on Jan 26, 2008 (gmt 0)

    10+ Year Member



    wow, thank you so much.

    Considering that of all of my keywords, none of them get over three words, so Ill just use that code up to three.

    thanks again.

    phranque

    1:55 pm on Jan 26, 2008 (gmt 0)

    WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



    welcome to WebmasterWorld [webmasterworld.com], adambeazley!