Help with this (simple) regular expression...? - asp.net

I'm using URL rewriting with my APS.NET application and haven't had much luck matching the following regular expressions...
<rewrite url="~/deals/(.+)$" to="~/Deals.aspx?deal_string=$1" />
<rewrite url="~/deals/(.+?)/edit$" to="~/EditDeals.aspx?deal_string=$1" />
I'm wanting a separate page for viewing a 'deal' to editing a 'deal'. I would like the URL to simply add '/edit' to a deal to go to the Edit Deal page.
Currently, all traffic goes to the first page.

The problem is that any url that matches the second one also matches with the first on (with a different $1 capture). Perhaps if you invert the sequence of declarations it will work. If the second and most specific is evaluated first it should do the job.
Also you could rewrite your expressions avoiding slash characters among your capture.
<rewrite url="~/deals/([ˆ/]+)$" to="~/Deals.aspx?deal_string=$1" />
<rewrite url="~/deals/(.+?)/edit$" to="~/EditDeals.aspx?deal_string=$1" />
The charset [ˆ/] means any char but /. The way the 1st expression would no longer match /deals/hello/edit.

Related

How to do URL rewriting for the URL to hide filename.aspx and querystring?

https://mysite.com/Admin/Dashboard/DashboardReport.aspx - this is my actual URL i want to hide the filename.aspx with if any querystring values. how?
also i have tried FriendlyURL in URL rewriting in IIS.
it doesn't take effect.
Pls give me a quick reply.
You want to use rewrite rules and the rewrite module.
http://learn.iis.net/page.aspx/461/creating-rewrite-rules-for-the-url-rewrite-module/
URL Rewriting is somewhat ambiguous. Are you rewriting what the user sees? Or what the server sees? It's better to believe that you are not able to alter what the URL displays to the website consumer.
However, the trick is to send them to a broken, not-real URL... then ReWrite the URL for YOU, so you know what that not-real URL means. For example:
Imagine you have a Real URL like so:
http://www.example.com/folder/subfolder/page.ext?param=value
But you want to "Rewrite" the URL to look like so:
http://www.example.com/subfolder/value
If they are on the page page.ext..., you cannot change what they see in their URL bar. But, you can Redirect Them to a fake path /subfolder/value like so:
<rule name="subFolder Value Redirect">
<conditions>
<add input="{REQUEST_URI}" pattern="^/folder/([^/]+)/page\.ext\?param=([^&]+)$" />
</conditions>
<action type="Redirect" url="/{C:1}/{C:2}" />
</rule>
Above, I am not doing a "url match" because I am depending solely on a Condition. My only Condition is that the Request_URI looks exactly like I expect. Some folder, some subfolder name, page.ext, and a querystring with param=SomeValue. I use () parenthesis to dynamically capture the Subfolder Name and the param Value.
Note: {C:0} means the "0th" Capture Group of the Conditions. Capture 0 is the Entire Result of the regex ^/folder/([^/]+)/page\.ext\?param=([^&]+)$. Capture 1 is the First Capture Group, the Subfolder Name. Capture 2 is the second Capture Group, the param Value.
^ at the beginning of a regex means "starting from the beginning". Sometimes you don't want to start at the beginning and that wouldn't be there.
$ at the end of a regex means "ending at the end". Sometimes you don't want to end at the end and that wouldn't be there.
The Character Class [^...] means to capture any character that is not .... In this case, capture anything that is not a Slash, or anything that is not an Ampersand.
The repetition operator [...]+ Plus-Symbol means to capture the preceding rule at least once, but up to many times.
. is a special character which means "anything". But we mean it as in the dot of an extension page.ext. So you escape it like \.
{REQUEST_URI} means to compare your pattern against the entire URL after the Domain/Port. So in the URL http://www.example.com:1234/subfolder... The /subfolder and all following contents are in the {REQUEST_URI} EXCEPT hashtag routing. So if you have an Anchor or Angular UI Route, those stay with the browser only, and will always be at the end of any redirect, but the server will never see them, so you can't parse off of hashtags.
action type="Redirect" just means this action is to redirect the user to a given URL.
url="/{C:1}/{C:2}" is the URL relative to the domain, since it doesn't start with a protocol. This will redirect to /subFolder/value, because Condition capture 1 and Condition capture 2 in our pattern capture those two values.
After inserting this rule, a user will be redirected to a broken janked up URL that looks like a State-Representative URL (RESTful). Yaay!!
URL Rewriting
The rewrite is for you (the server), not for the customer. But now that they are redirected to a nice pretty broken URL, you can find the context of that URL and tell your server what it REALLY means by rewriting it for the server.
<rule name="subFolder Value Rewrite">
<conditions><add input="{REQUEST_URI}" pattern="^/([^/]+)/([a-zA-Z0-9]+)$" /></conditions>
<action type="Rewrite" url="/folder/{C:1}/page.ext?param={C:2}" />
</rule>
This is pretty much the inverse of the prior rule. This finds a pattern where there is a path "subFolder" name, slash, and a value made up of letters and numbers. It also, once again, describes the Begginning ^ and End $, because we don't expect unexpected weird things. So, this will catch a URL like:
http://www.example.com/subFolder/1234abcDE
But will not catch
http://www.example.com/lol/somethingelse/15?blah=kthx
The rewrite URL then evaluates back to a true real URL that maps to a path for processing for a file that really exists. And, keep in mind, that "rewrite" means it takes the "incorrect" fake value that the user sees, and rewrites it back to an ugly "correct" value for the server. Not the other way around.

debugging Url rewriting in umbraco

I have a url rewrite rule specified for my umbraco site like:
<add name="circuitdetail"
virtualUrl="^~/circuits/(.*).aspx"
rewriteUrlParameter="ExcludeFromClientQueryString"
destinationUrl="~/circuits/detail.aspx?circuit=$1"
ignoreCase="true" />
I would like the /circuits/albert-park to be mapped to /circuits/detail?circuit=albert-park.
When I enter that second url I correctly get the desired page but when I enter the first, I get my 404 page.
In Umbraco I have a page named "Circuits" and nested underneath it is the page named Detail. As described above, entering the unfriendly url works but it seems the url rewrite is not working.
What am I doing wrong?
It looks as if your RegEx is matching the destinationURL as well as the VirtualURL.
So when it re-writes to ~/circuits/detail.aspx?circuit=$1
This is also being matched by the ^~/circuits/(.*).aspx RegEx.
Adding a $ to the end of your RegEx should fix it. The $ tells the RegEx to match up to the end of the line.
i.e.
^~/circuits/(.*).aspx$
More info: http://www.regular-expressions.info/anchors.html

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

URL ReWriter for .NET

I am using this, but I have some hard to solve problems.
This is part of my code:
<rewrite url="/Dictionary/(.+)/(.+)" to="~/Dictionary.aspx?page=$2&&word=$1"/>
<rewrite url="/Dictionary" to="~/Dictionary.aspx"/>
When I type links like mywebsite.com/Dictionary/cat/4 the site loads only mywebsite.com/Dictionary.
Just a guess. Your second condition matches everything starting with /Dictionary.
You probably want this
/Dictionary/?$
Just a suggestion but you can also try out IIS Url Rewrite 2 instead.
URL Rewrite 2 is a good option like XII said, comes with an user interface.
Regarding the regex, it would be more efficient for the regex engine to avoid backtracking; use the following expression instead:
"/Dictionary/([^/]+)/([^/]+)"

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