how to use url rewriter.net to auto rewrite urls - asp.net

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

Related

Nginx rewrites for URL that must have a dash

This one allows everything with .html extension that contains no slashes:
rewrite ^/([^/]+).html$ ...
I need to add another catch to it: URL must contain at least one dash, then it can be rewritten.
How to do that?
Just use logic. Word with at least one dash could be expressed as two words with dash between them. So solution is simple:
rewrite ^/([^/]+-[^/]+)\.html$.
Also you forgot to escape dot (.) so your regexp also match url /somesstrangehtml

Extract subdomain from url using regex

I've searched through all of the related topics here but none seems to answer my specific need. Here is the problem: Given a URL (sans protocol), I want to extract the subdomain portion, excluding www. The domain portion is always the same so I don't need to support all TLDs. Examples:
www.subdomain.domain.com should match subdomain
www.domain.com should match nothing
domain.com should match nothing
This is one of the many iterations I have tried:
[^(www\.)]\w+[^(\.domain\.com)]
Square brackets indicate character class and will remove all the order of otherwise special meaning of most characters.
You can try something like this instead:
((?:[^.](?<!www))+)\.domain\.com
regex101 demo
To return what you're looking for instead of retrieving it through submatches:
((?:[^.](?<!www))+)(?=\.domain\.com)
regexp101 revised

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

ISAPI rewrite (using Helicon on IIS and ASP.NET) and dynamic query string append?

Is it possible, using regex, to parse the query string and build a nice URL that can be translated back to the ugly URL for use on a legacy application? For example, let's say we are not certain what query string parameters are going to be part of a URL. Here is an example:
http://www.domain.com/thePage.aspx?Param1=1&Param2=23&Param7=22
We want to rewrite the URL as
http://www.domain.com/Param1/1/Param2/23/Param7/22
At the same time we need the filter to allow the application to see it as the old URL to be processed correctly. Again, we don't know what combination of parameters may be used.
Can this be done only using the filter?
As official forums said, you can use RewriteCompatibility2 on in Helicon 3.0, so you can use a LP directive (looping with the replace).
I use this ReGex to parse QueryString (it makes two groups: name with all parameter names, and value with all parameter values)
[\?&](?<name>[^&=]+)=(?<value>[^&=]+)
Based on mentioned forum topic, I think you should try something like this:
//for replacing '='
RewriteRule ^[\?&]([^&=]+)=([^&=]+)$ $1/$2 [LP,R=301,L]
//for replacing '&'
RewriteRule ^\?([^&]+)&([^&]+)$ $1/$2 [LP,R=301,L]

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