Url Rewriting Trouble

  • Thread starter Thread starter Roshawn
  • Start date Start date
R

Roshawn

I'm using the free UrlRewriter.Net HttpModule to perform url rewrites on my website.
Well, sort of. The thing is, I can't get the rewrites to work.

After creating a configSection and adding the component to the httpModule section of my
web.config file, I began writing rules. Here's a sample of what I have:

<rewriter>
<rewrite url="~/Products/(.+)" to="~/Products/default.aspx?category=$1"/>
<rewrite url="~/Products/(.+)/(\d+)"
to="~/Products/default.aspx?category=$1&amp;page=$2"/>
</rewriter>


While testing the first rule, everything works perfectly. The second rule, unfortunately,
screws up. Instead of having two querystring parameters, ASP.NET only sees one. For
example, suppose a customer had "~/Products/CDs/3" in their address bar. ASP.NET doesn't
see it as "~/Products/default.aspx?category=CDs&page=3" but rather views it as
"~/Products/default.aspx?category=CDs/3"

Why isn't this working? Is my use of regular expressions wrong (I'm not good at them)?
Please help. :(

Roshawn
 
Roshawn said:
I'm using the free UrlRewriter.Net HttpModule to perform url rewrites on my website.
Well, sort of. The thing is, I can't get the rewrites to work.

After creating a configSection and adding the component to the httpModule section of my
web.config file, I began writing rules. Here's a sample of what I have:

<rewriter>
<rewrite url="~/Products/(.+)" to="~/Products/default.aspx?category=$1"/>
<rewrite url="~/Products/(.+)/(\d+)"
to="~/Products/default.aspx?category=$1&amp;page=$2"/>
</rewriter>


While testing the first rule, everything works perfectly. The second rule, unfortunately,
screws up. Instead of having two querystring parameters, ASP.NET only sees one. For
example, suppose a customer had "~/Products/CDs/3" in their address bar. ASP.NET doesn't
see it as "~/Products/default.aspx?category=CDs&page=3" but rather views it as
"~/Products/default.aspx?category=CDs/3"

Why isn't this working? Is my use of regular expressions wrong (I'm not good at them)?
Please help. :(

The (.+) is greedy (yes this the technical term for it) and gobbles up the
/ and everything after it

You can use (.+?) to stop it being greedy or use ([^/]+) which keeps
matching characters until it finds a / .
 
Thanks for the reply, Anthony.

Trying your first suggestion (i.e. (.+?)) produced the same result as the first. Your
second suggestion (i.e. ([^/]+)) allows only the category parameter to be read; the page
parameter gets ignored completely.

I tried using the second suggestion with an underscore character. I was able to get the
page parameter, but the category parameter changed to
"~/Products/default.aspx?category=$1" IOW, the category parameter contained the
directory, page name and all.

I appreciate your trying to help me. You've given me more than I had before. Hopefully I
can get things working properly.

Thanks again,
Roshawn

Anthony said:
Roshawn said:
I'm using the free UrlRewriter.Net HttpModule to perform url rewrites on my website.
Well, sort of. The thing is, I can't get the rewrites to work.

After creating a configSection and adding the component to the httpModule section of my
web.config file, I began writing rules. Here's a sample of what I have:

<rewriter>
<rewrite url="~/Products/(.+)" to="~/Products/default.aspx?category=$1"/>
<rewrite url="~/Products/(.+)/(\d+)"
to="~/Products/default.aspx?category=$1&amp;page=$2"/>
</rewriter>


While testing the first rule, everything works perfectly. The second rule, unfortunately,
screws up. Instead of having two querystring parameters, ASP.NET only sees one. For
example, suppose a customer had "~/Products/CDs/3" in their address bar. ASP.NET doesn't
see it as "~/Products/default.aspx?category=CDs&page=3" but rather views it as
"~/Products/default.aspx?category=CDs/3"

Why isn't this working? Is my use of regular expressions wrong (I'm not good at them)?
Please help. :(

The (.+) is greedy (yes this the technical term for it) and gobbles up the
/ and everything after it

You can use (.+?) to stop it being greedy or use ([^/]+) which keeps
matching characters until it finds a / .
 
Roshawn said:
Thanks for the reply, Anthony.

Trying your first suggestion (i.e. (.+?)) produced the same result as the first. Your
second suggestion (i.e. ([^/]+)) allows only the category parameter to be read; the page
parameter gets ignored completely.

I tried using the second suggestion with an underscore character. I was able to get the
page parameter, but the category parameter changed to
"~/Products/default.aspx?category=$1" IOW, the category parameter contained the
directory, page name and all.

I appreciate your trying to help me. You've given me more than I had before. Hopefully I
can get things working properly.

This must be something to do with how the product works. I've tested my
suggestions as regular expressions and they clearly work. I wonder whether
the string parsing requirements of the product include the need to escape
the \ character. Try this:-

"~/Products/([^/]+)/(\\d+)"

Note the \\ infront of the d.

In fact the greedy version works with that mod but I would use the above.
 
You can try IIRF - another rewriting filter. It also uses regular
expressions.
www.codeplex.com/IIRF.
(free)


Anthony Jones said:
Roshawn said:
Thanks for the reply, Anthony.

Trying your first suggestion (i.e. (.+?)) produced the same result as the first. Your
second suggestion (i.e. ([^/]+)) allows only the category parameter to be read; the page
parameter gets ignored completely.

I tried using the second suggestion with an underscore character. I was able to get the
page parameter, but the category parameter changed to
"~/Products/default.aspx?category=$1" IOW, the category parameter contained the
directory, page name and all.

I appreciate your trying to help me. You've given me more than I had before. Hopefully I
can get things working properly.

This must be something to do with how the product works. I've tested my
suggestions as regular expressions and they clearly work. I wonder whether
the string parsing requirements of the product include the need to escape
the \ character. Try this:-

"~/Products/([^/]+)/(\\d+)"

Note the \\ infront of the d.

In fact the greedy version works with that mod but I would use the above.
 
Back
Top