URL Rewrite in IIS7 Regular expression Pattern - asp.net

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.

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.

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}

Problem in URL validation using ASP.Net Regular Expression Validator

I'm trying to use the ASP.Net Regular Expression Validator to validate a URL field. URL is www.tachibana.co.jp/tokyosys.htm. Validation expression used is ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?" but this is not working. Is there anything wrong with the Regular expression or URL ?
Rules are as below.
It should validate even if (http or
https) is included or not.
It should also trim the URL before
validating.
It should also validate the sub
domain URL's
It should also validate the URL's to
a file on domain or sub domain.
thanks
The problem is that your regex
http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
expects the URL to start with http:// or https://. Also, the dash inside the character class is misplaced.
Edit: Now that you've posted your rules, I suggest this:
^\s*((?:https?://)?(?:[\w-]+\.)+[\w-]+)(/[\w ./?%&=-]*)?\s*$
After a successful match, group 1 will contain the domain, and group 2 will contain the file path, if present.
^(?i)(http|ftp|https)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$
"(http(s)?://)?([\www]+\.)+[\w-]+(/[\w- ;,./?%&=]*)?"
var re = /(http(s)?:\\)?([\w-]+\.)+[\w-]+[.com|.in|.org]+(\[\?%&=]*)?/
if (re.test(txt)) {
alert('Valid URL')
}
you can add domain needed in the last field of com,in,org

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

Intelligencia URL ReWriter mapping with regex

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

Resources