Need help with Rewrite rule

Topics: User Forum
Aug 1, 2009 at 9:48 PM

My appologies if this is a simple question....Please help me with the rewrite rule for the below condition

I want  to be rewritten as

and  to




Aug 1, 2009 at 10:37 PM

yes, it's a simple question. There are examples in the doc that cover this. but I know it's a huge inconvenience to actually read the documentation. so you ask me. let's see...

  # convert to
  RewriteRule  ^/([^\./]+)$    /default.aspx?id=$1  [L]  

  # convert to
  RewriteRule  ^/([^\./]+)/info$    /info.aspx?id=$1  [L]  

Ahhhh - I made a change to your request. Instead of exposing info.aspx on the 2nd type of URL, I made it just info. No need for a .aspx on that public URL. You can add the .aspx if you like, by inserting it back in the pattern at the appropriate spot. In case you read none of the IIRF documentation, the "pattern" is the first argument to the RewriteRule directive.

Also you didn't describe the pattern for "eventname", so I used "any sequence of one or more characters, where each character is neither a dot nor a slash". this looks like "([^\./]+)" in a regex.  If you want a stricter definition for the eventname thing, you'll have to change that regex.

Here's the thing: as they say, a little knowledge is dangerous.  You now have a couple rules that do "basically" what you asked for.  But the internet is a hostile place.  You will get "good" requests for the URLs that match those rules, and those requests will be rewritten as you want. But you will also get a ton of "bad" URLs that also match those patterns.  You may want to filter those bad URLs.  And, there are probably many other kinds of URLs you will want to handle.  You should run tests to satisfy yourself that everything works.

You also need logging, and you need to decide on a log level . You should insert the StatusUrl directive.  There are a bunch of other things to consider.

I can't give you the answers for that stuff.  You have to do it yourself.


Aug 1, 2009 at 11:51 PM

Thank you so much. 

Can you please help me with the below condition [I'm a bit slow with RegExp :(  ]


Aug 2, 2009 at 12:40 AM

what do you think the answer is?

Aug 2, 2009 at 4:27 PM
How about this                    ^/([^\./]+)/info.aspx?([^\./]+)=([^\./]+)$    /info.aspx?$2=$3&id=$1  [L]  

Aug 2, 2009 at 6:01 PM
Edited Aug 2, 2009 at 6:12 PM

How about this

  RewriteRule  ^/([^\./]+)/info\.aspx   /info.aspx?id=$1 [L,QSA]

This pattern matches a URL with a single prefix segment, followed by info.aspx. (You have to escape the dot, with a slash.) There may be a query string that follows info.aspx, but for now we don't care about it. The replacement says, whatever the first segment is, gets transformed into a querystring param, using the id name. Now, what about the original query string? In your case, it was "infoid=1". The QSA modifer on the rule says "append any query string that was on the original URL."

Now, if you don't want to append just any query string, then you should go back to your idea, where you parse the incoming query string into parts.  But your regex doesn't quite work.  The pattern ([^\./]+) means "a sequence of multiple characters, none of which is a dot or slash".  But for a query string param, I think you want "a sequence of multiple characters, none of which is is a n equals sign." Something like this:  ([^=]+) .  So

RewriteRule  ^/([^\./]+)/info\.aspx\?([^=]+)=([^=]+)$    /info.aspx?$2=$3&id=$1  [L]

You should run a bunch of tests to make sure this set of rules works for all good and bad URLs.

Aug 4, 2009 at 1:09 AM

Thank you so much, your explanation was very helpful.

When I tested RewriteRule ^/([^\./]+)/info\.aspx /info.aspx?id=$1 [L,QSA]

/products/info.aspx resulted as /info.aspx?id=products&Noserver-Query_String

/products/info.aspx?url=1 resulted as error with No Action (expected: /info.aspx?id=products&url=1)

Can you please help me figure out the problem.



/products/picturegallery.aspx   /picturegallery.aspx?en=products 
/products        /default.aspx?en=products
/products/picturegallery.aspx?url=1 /picturegallery.aspx?en=products&url=1  



Aug 4, 2009 at 2:40 AM

The handling of the QSA flag uses the server variable "QUERY_STRING".  But,  in the test tool, there is no server, and so there are no server variables.  The result is you get that weird string, "NoServer-QUERY_STRING".   To really test this, you need to run tests within the server.