Intelligencia URL ReWriter mapping with regex - asp.net

I am using the Intelligencia URL rewriter in my asp.net web application.
I use the web.config mappings
I'm trying to map the following url:
www.mydomain.com/product-deals/manufacturer-model_PRODUCTId.aspx
To:
www.mydomain.com/ProductInfo.aspx?productID=xxx
obviously in the above example, xxx is replaced from the "productId" from the "friendly" url.
In my web.config, I've got so far:
<rewrite url="~/contract-deals/([\w-_]+)/_(.+).aspx" to="~/ProductInfo.aspx?productId=$1"/>
This isn't working however.
I need the correct regex to use for my requirements (regex really isn't my strong point!!)

One problem is that you have product-deals in your sample and contract-deals in the regex.
Next, your regex has an extra slash, and you don't escape the dot (though it can match a dot anyway). Also, $1 refers to the first capturing group, which in your case is "manufacturer-model".
This regex should get you what you want:
product-deals/[\w_-]+_(.+)\.aspx

Related

URL rewriting: Should "?" or "&" be used for additional parameters

I have the following URL rewriting rule (IIS 7.5 URL Rewrite module/ASP.NET) in place:
www.domain.com/page.html?id=1
www.domain.com/page/1
I want to add an optional tracking parameter, p=1, to the rewritten URL. Should I use a ? or an &?
www.domain.com/page/1?p=1
or
www.domain.com/page/1&p=1
Both seem to work, I'd just like to know the "correct" way to do this to avoid any compliance/compatibility issues.

URL Rewrite in IIS7 Regular expression Pattern

I want a URL pattern for the following where :
this
http://www.test.com/xyz_number.jpg?vin=xyz&date=31052012
will be redirected to :
http://www.test.com/xyz/31052012/xyz_number.jpg
NOTE: Here xyz_number and date value are dynamic which will be changing for each request.
The following regular expression will perform the match:
^http://www.test.com/([^.]*).jpg\?vin=([^&]*)&date=(\d*)$
And the following expression will do the replacement:
http://www.test.com/$2/$3/$1.jpg
You don't say whether you're doing this in an ASP.NET HTTP Handler, or in an IIS module, but hopefully, this will give you a start.

IIS 7 URL rewrite on WCF Service

Question Edited for better understanding:
I have a WCF service and any of my links look like :
https://192.168.1.31/ContactLibrary2.0HTTPS/Service.svc/..... .
I want to get rid of the Service.svc. I installed URL Writer in IIS but i don't know how to work with it. I search a little bit and didn't find anything to help me with this particular problem.
Any idea ?
Assuming you are configuring the application hosted at /ContactLibrary2.0HTTPS directly (and not the website containing that directory, for example), you may add an exact match for:
rest/GetContact
with a rewrite url of:
Service.svc/rest/GetContact
Perhaps you wish to rewrite every action of Service.svc, however; then you would need a regular expression match for:
^rest/.*$
with a rewrite url of:
Service.svc/{R:0}
UPDATE
Assuming you also need to remove that string from the urls of your HTML pages, you would need to couple the aforementioned inbound rule with a new outbound rule, applied to the files you are interested in.
To do that, please:
add a new outbound rule to your website and give it a name;
add a new precondition with two rules (matching any of them):
{RESPONSE_CONTENT_TYPE} matches text/html
{RESPONSE_CONTENT_TYPE} matches application/xhtml+xmll
configure the rule to match the response scope, matching the content within A tags:
should match the pattern using a regular expression;
with this pattern: ^(.*)(/Service\.svc/)(.*)$
case insensitive;
configure the action to be a rewrite, with this value: {R:1}{R:3}

how to use url rewriter.net to auto rewrite urls

i am trying to rewrite any URL that match this pattern:
~/Ahmed
~/Name
to this:
~/User/Ahmed/Ahmed.aspx
~/User/Name/Name.aspx
and i can write them individually but what i am trying to do is detect any URL that look like "~/User/Ahmed/Ahmed" and auto rewrite them to this "Ahmed"
thanks
Hopefully you're using the UrlRewritingNet library, not UrlRewriter? The former is suggested over the latter.
However, in either you can use a regex:
"~/User/([^/\\]+)/\1.aspx" -> "~/$1" //For ".aspx" in the URL
"~/([A-Za-z]+)" to "~/User/$1/$1.aspx" //For /Name in the URL.
Note the ([^/\]+) means any set of characters without slashes,
and "\1" is a backreference to the previous capture, that ensures the name is an exact duplicate. Note that you should enable "ignore case" if you want to support "/User/ahmed/Ahmed.aspx" and not just "/User/Ahmed/Ahmed.aspx".

Why URLRewriter.NET doesn't get querystring values?

I just started using URLRewriter.net with my blog and I have a problem with getting the query string values. I have a rule setting like:
<rewrite url="~/blog.aspx(\?.+)?$"
to="~/hiddenFolder/blog.aspx?mode=default&$2"/>
But when I try to access /blog.aspx?page=1 the page parameter is not passed. Other parameters work great and there are no conflicts in rewriting rules.
I think the problem is that $2 is out of range as you only have one group in your RegEx. Try $1.
EDIT
In addition, it could be that the query string is being appended with another '?' so you need to move that out of the brackets.
You'll also need an extra group to make the rule match with our without the '?'. Note: we're back to $2 in the result now :)
<rewrite url="~/blog.aspx(\?(.+)?)?$"
to="~/hiddenFolder/blog.aspx?mode=default&$2"/>

Resources