Url Rewriting Trouble

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
 
A

Anthony Jones

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 / .
 
R

Roshawn

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 / .
 
A

Anthony Jones

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

Cheeso

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Help About URL Rewriting 2
form action attribute + url rewriting 0
Changing .html in URL 3
url rewriting 3
url rewriting 3
URL Rewriting and file paths 9
URL Rewriting 2
URL Rewriting and sub folders 0

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,117
Latest member
Matilda564
Top