calling urlrewrite from code - asp.net

I have a url as a string and I want to call the asp.net code (IIS7 module) that rewrites urls on their way into my application and get a rewritten url from it:
http://webserver/nice/url/youHaveThere
to
http://webserver/app/Default.aspx?category=nice&catalog=url&pageid=youHaveThere
I want to call this function from inside an asp.net application. How do I do it?

I'm assuming you have the module already installed on your server, and are just asking about defining a specific rule. This is where you can read everything you need about creating rules.
The rules are all done through regular expressions, not my forte. But this is a format example:
<rewrite>
<rules>
<rule name="Rewrite to article.aspx">
<match url="^article/([0-9]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="article.aspx?id={R:1}&title={R:2}" />
</rule>
</rules>
</rewrite>
The ([0-9]+) stands for number only patterns, ([_0-9a-z-]+) stands for text and number. I would think this would work for what you're tying to:
<rewrite>
<rules>
<rule name="Rewrite to Default.aspx">
<match url="^article/([_0-9a-z-]+)/([_0-9a-z-]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="app/Default.aspx?category={R:1}&catalog={R:2}&pageid={R:3}" />
</rule>
</rules>
</rewrite>

I wouldn't think you'd call a "rewrite rule". Instead you'd just use a Regex Replace. Maybe something like:
string nice_url = "http://webserver/nice/url/youHaveThere"
string new_url = Regex.Replace(nice_url, #"/(?<category>\w+)/(?<catalog>\w+)/(?<pageid>\w+)$", "app/Default.aspx?category=${category}&catalog=${catalog}&pageid=${pageid}");

Related

How to convert asp.net rewrite rule to .net core rewrite rule

In Asp.Net I had a rewrite rule to abort processing when we encounter the tilde character to avoid IIS directory enumeration like this
<rewrite>
<rules>
<rule name="RequestBlockingRule1" patternSyntax="Wildcard" stopProcessing="true">
<match url="*"/>
<conditions>
<add input="{URL}" pattern="([^\?].?.$)|([^\?].*$)"/>
</conditions>
<action type="AbortRequest"/>
</rule>
</rules>
</rewrite>
And so far in .net core I have this in program.cs
var options = new RewriteOptions().AddRewrite("([^\\?].?.$)|([^\\?].*$)","",true);
So I am matching the same pattern with regex but how do I tell it to abort/stop processing? I dont want to replace it with something else.
Looking at microsoft documentation but not sure yet..
Thanks

IIS URL rewrite dynamic number of folders

I have to create a rewrite rule that catches all URLs from the old shop system and redirect them to the new shop. The problem is the following:
The source URL from the old shop can contain the path where you did find the product number, looks like this: www.domain.com/folder1/folder2/folder3/[product number].html
folder1, folder2 and folder3 can, but don't need to be in the called URL. They also are not fixed (e.g. folder1 can be "cars" or "bikes" or "services"). The link could also be just www.domain.com/folder1/[product number].html or even www.domain.com/[product number].html sometimes.
For the new system, I only need the [product number] in my target URL. Should look like this: www.domain.com/path1/path2/[product number].aspx
I could not find anything in Google or Stackoverflow that helped me with this.
Thanks in advance
Daniel
This should work:
<system.webServer>
<rewrite>
<rules>
<rule name="Three deep" stopProcessing="true">
<match url=".*/.*/.*/(.*?).html" />
<action type="Redirect" url="/path1/path2/{R:1}.aspx" />
</rule>
<rule name="Two deep" stopProcessing="true">
<match url=".*/.*/(.*?).html" />
<action type="Redirect" url="/path1/path2/{R:1}.aspx" />
</rule>
<rule name="One deep" stopProcessing="true">
<match url=".*/(.*?).html" />
<action type="Rewrite" url="/path1/path2/{R:1}.aspx" />
</rule>
<rule name="None deep" stopProcessing="true">
<match url="(.*?).html" />
<action type="Rewrite" url="/path1/path2/{R:1}.aspx" />
</rule>
</rules>
</rewrite>
</system.webServer>
I am worried that the expression - (.*?) - I'm using for the product code is too catch all, but I don't know the structure of your product codes. If you could tighten it up, it'd be better.
I'm also worried that they will hit things that aren't products (e.g. category and search) so if there is a pivot point that all product URLs use, I'd recomend using that. Tightening the product code bit will probably help false matches too.
At the moment it's working on the .html vs .aspx being the bit that isn't sending it into a redirect spiral.
I can probably do it in less rules with a better regex, which I'll have a bash at tonight.

Outbound IIS Rewrite Rule not adding Parameter, what am I missing?

I want to use IIS Rewrite to change /blog.aspx?Page=2 to /blog?Page=2 but as its the address is in a DataPager, its not so simple. TO get round this, I've come up with the following IIS rewrite rule.
<outboundRules>
<rule name="BlogArticlesOutbound" stopProcessing="true">
<match filterByTags="A" pattern="^/blog.aspx\?page=([0-9]+)" />
<action type="Rewrite" value="/blog?page={R1}" />
</rule>
</outboundRules>
However, the output is missing the page number, what do I need to do to change it to get it to work? My style sheet seems to also disappear from the page when i use the rule.
Your back-reference syntax is a tiny bit wrong. You should use it as {R:1}.
<outboundRules>
<rule name="BlogArticlesOutbound" stopProcessing="true">
<match filterByTags="A" pattern="^/blog.aspx\?page=([0-9]+)" />
<action type="Rewrite" value="/blog?page={R:1}" />
</rule>
</outboundRules>

Asp.net allow access only through routes?

I am using URL rewrite to make user-friendly URL's. So www.mysite.som/search is being handled by www.mysite/search.aspx correctly. But there is one more thing I want. I want to prevent direct access to my page search.aspx. So, if someone enters www.mysite.com/search.aspx, he should be redirected to www.mysite.com/search instead.
What have I tried?
<rewrite>
<rule name="Redirect to friendly" stopProcessing="true">
<match url="^(.*)\.aspx$" />
<action type="Redirect" url="{R:1}" />
</rule>
</rewrite>
But its not working.

Create a rule with Service.Transfer() Behaviour IIS Rewriting Module

Is it possible to create a rule that has a behaviour similar to the Service.Transfer from ASP ?
Using IIS7 Rewriting Module you can use a Rewrite Action.
This keeps the original URL but rewrites the path which your application will see and process in a similar way to Server.Transfer.
<rewrite>
<rules>
<rule name="Rewrite to article.aspx">
<match url="^article/([0-9]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="article.aspx?id={R:1}&title={R:2}" />
</rule>
</rules>
</rewrite>
Have a look at the rules overview.
This essentially is the same as Context.RewritePath.

Resources