1

Scratching my head on this. I have an old URL:

     http://myserver/blog/archive?openview&type=Category&key=Demo

and a new URL

     http://myserver/blog/categories/Demo

I'm trying to get a rewrite rule for this. I thought it would be:

     ^/blog/archive\?openview&type=Category&key=(.*) /blog/categories/$1.html [NC,R=301,L]

But that doesn't do anything. I tried the URL and rule (replace the ? with /):

     http://myserver/blog/archive/openview&type=Category&key=Demo
     ^/blog/archive/openview&type=Category&key=(.*) /blog/categories/$1.html [NC,R=301,L]

And that works as expected. So I'm a bit dumbfolded how to handle the ? for matching in the old URL. All the samples I found describe the opposite case. Sending the "pretty" URL to the ugly one. In my case the "ugly" URL is what people in the past had bookmarked and I want to make sure they get to the new data properly

1
  • Excellent link. Thxs for adding it here!
    – stwissel
    Feb 20, 2014 at 17:21

1 Answer 1

2

A little thinking (and reading the documentation) later:

The mod_rewrite matches use the location part of the URL only. The "?" marks the end of the location and the beginning of the parameters (a.k.a QueryString). Once I realized that, it was just a Google away.

So to transform correctly you need a rewrite condition:

RewriteCond %{QUERY_STRING}     ^openview&type=Category&key=(.*)    [NC]
RewriteRule ^/blog/archive$ /blog/categories/%1.html [NC,R=301,L]

Works like a charm

Not the answer you're looking for? Browse other questions tagged .