IIS 7 URL Rewrite Match URL - iis-7

I'm trying to set a canonical default URL in IIS 7 using the URL Rewrite module. I think that I'm misunderstanding how the 'Match URL' field is used. The following doesn't seem to do anything:
<rewrite>
<rules>
<rule name="EnforceDefaultPage">
<match url="^http://(?:www\.)?mydomain\.com(?:/)?(?:blog\.aspx)?$" />
<action type="Redirect" url="http://www.mydomain.com/blog" appendQueryString="false" />
</rule>
</rules>
</rewrite>
I've noticed in a lot of examples that people have added a condition utilizing the HTTP_HOST variable... but how does this relate to the match url? It seems that I should be able to omit any conditions because my regex matches exactly what I want.

Ahh, I've finally figured it out. Apparently how 'much' of the URL is available for matching depends on the location of the web.config in the directory hierarchy. Since I was placing the code in the web.config in the web root, it could only match anything after the domain name (i.e. it can match everything after 'blog.com/' in 'http://www.blog.com/').
I found the answer here: http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference
"Note that the input URL string passed to a distributed rule is always relative to the location of the Web.config file where the rule is defined. For example, if a request is made for http://www.mysite.com/content/default.aspx?tabid=2&subtabid=3, and a rewrite rule is defined in the /content directory, then the rule gets this URL string default.aspx as an input."

Related

Problem to rewrite an old URL to a new one in IIS manager and web.config file

I'm here in last resort. So here is my problem,
At work my boss asked me to rewrite an URL to be more user friendly.
The old pattern is this one: https://{hostServer}/SiteName/index.html
The new one should be this one: https://maps.swcs.be
But i looked in the doc of IIS, looked in here for some more informations etc...
I learned, it's done in the web.config file at the root of the repo "wwwroot" So I wrote my rule in there
here it is
<system.webServer>
<rule name="redirection d'url">
<match url=".*"/>
<conditions>
<add input="{https://maps.swcs.be}" type="Pattern" pattern="Ëxpertises[0-9]\index\.html">
</conditions>
<action type="Rewrite" url="https://maps.swcs.be" />
</rule>
</system.webServer>
I tried the <action type="Redirect" ...> too and other stuff but it's the only one that returned something... the IISstart.htm file
I'm pretty new to URL redirection, if one of you could enligthen me that would be awesome
Thanks in advance
First of all, I highly recommend that you read the documentation provided by lex li in detail, which can help you fully understand how to use url rewrite.
Secondly, I want to know your initial url and rewritten url, you don’t have to show the correct url, just an example. Such as initial url is http://example.com/aaa/bbb/ccc/index and rewritten url is http://example.com/index
So I assume your initial url is https://{hostServer}/SiteName/index.html and rewritten url is https://maps.swcs.be.
This rule will match the uri, if the uri is /SiteName/index.html, it will redirect to https://maps.swcs.be. You can also change antion type to rewrite. If you choose rewirte, nothing will happen when you check url in browser. But redirect will make the url change to new one in browser.

Regex to extract a part out of a URL and use it as a reference in url rewrite

OK, I'm wondering if someone can lend a hand with a regex I'm trying to write.
Basically, what I want to do is use IIS urlrewrite module to do a redirect to a specific URL if the user accesses a URL on another site. The only catch is I have to also capture a bit of the query string, and move it into the redirect. I think I actually DO have the regex correct for this, but it's implementing it in my web.config that is causing the problem.
so here is the input, the URL that a user may access would look like:
https://of.example.com/sfsv3.aspx?waform=pro&language=en
I want to match that URL (either http or https, case insensitive), and capture from it also one piece of information, the two letter language code. then the url i want to forward the user to looks like:
http://example.com/ca/en/ppf
(where en is replaced by whatever i captured above)
So, I'm working with IIS Rewrite module, and I've gotten my input data and regex in, so far the regex pattern I have is this:
https?://of.example.com/sfsv3.aspx\?waform=pro&(language=(..))
so basically i'm matching the whole string, plus a group and a subgroup for language and it's code. in the IIS test pattern dialog, this is working.
I get the following
{R:1} language=en
{R:2} en
great! so then my IIS rewrite rule should look like this to redirect the user:
<system.webServer>
<rewrite>
<rules>
<rule name="test" stopProcessing="true" enabled="true">
<match url="https?://of.example.com/sfsv3.aspx\?waform=pro&(language=(..))" ignoreCase="true" />
<action type="Redirect" url="http://www.example.com/ca/{R:2}/ppf" />
</rule>
</rules>
</rewrite>
</system.webServer>
this all seems right to me. however, IIS is balking and saying that my web.config is invalid. specifically, it says "Entity '(' is not defined" and it's pointing to the ( right before language as the problem. So, I can't build or deploy this application till I fix that. If i build it without and then just try to drop that into the web.config, i get an error loading the site:
HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.
Can someone help me figure out how to put a capture group into a rewrite rule properly?
You forgot to code the ampersand. Should be:
<system.webServer>
<rewrite>
<rules>
<rule name="test" stopProcessing="true" enabled="true">
<match url="https?://of.example.com/sfsv3.aspx\?waform=pro&(language=(..))" ignoreCase="true" />
<action type="Redirect" url="http://www.example.com/ca/{R:2}/ppf" />
</rule>
</rules>
</rewrite>

URL Rewrite in IIS

Whenever a request
http://localhost:9000/Content/PDF/ABC.pdf
comes (in IIS), i want URL rewrite module to process that request and redirect it to
http://localhost:9000/User/GetPdf
so that my controller and action methods are invoked and my code gets executed before a pdf file is shown to end user.
Here User is UserController and GetPdf is an ActionMethod in my application.
Can any body tell me steps to create this URL rewrite.
Thanks in advance.
I'm assuming you are using IIS 7.x and you have installed URL Rewrite 2.0 module.
In your application's web.config file, add a <rewrite> element similar to:
<system.webServer>
<rewrite>
<rules>
<rule name="PDF Rewrite">
<match url="Content/PDF/([\w-]+)\.pdf" />
<action type="Rewrite" url="User/GetPdf/{R:1}" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
The ([\w-]+) portion of url="Content/PDF/([\w-]+)**\.pdf" will "capture" the name of the PDF ("ABC" in your example) without the file extension. The {R:1} portion of <action type="Rewrite" url="User/GetPdf/{R:1}" logRewrittenUrl="true" /> will then insert the captured string. The net result is that:
http://localhost:9000/Content/PDF/ABC.pdf
becomes:
http://localhost:9000/User/GetPDF/ABC
I am assuming that you will need to pass in the name of the PDF to your Action method, so if you implement the standard routing pattern {Controller}/{Action}/{id}, then the id will be set to "ABC".
The best introduction to the URL Rewrite module that I have found is actually the URL Rewrite 1.1 configuration reference. Even though it is for v.1.1 rather than 2.0, it provides a better overview than the v.2.0 Configuration Reference. It is worth reading from the start. In particular, one must understand the “Accessing URL Parts from a Rewrite Rule” section.
To debug the URL Rewrite module, you can use the IIS 7.x "Failed Request Tracing," which, contrary to its name, can be used to trace successful as well as failed requests. This www.iis.net entry illustrates how to configure this and amount of detailed debugging information that is available.
Also note that the logRewrittenUrl="true" attribute means that the rewritten URL will be logged to the standard IIS log instead of the original URL. (It can't be used to log both the original and the rewritten URL--- you can only get one or the other.)

IIS URL Rewrite based on root url

I'm trying to rewrite a url:
www.mydomain.com/urlfolder/filename.aspx
to point to
www.mydomain.com/urlfolder/newfile.aspx
My web config has the following:
<rule name="PageRedirection" enabled="true" stopProcessing="true">
<match url="urlfolder/filename.aspx" />
<action type="Redirect" url="urlfolder/newfile.aspx" redirectType="Permanent" />
</rule>
the problem is that this is catching urls such as:
www.mydomain.com/subdirectory/urlfolder/filename.aspx
I tried to change my url to be
but the ^ didn't work. It also seems ~/ doesnt work to specify the root either.
How would I go about specifying this url from the root, w/o putting in an absolute path.
I also have:
testsite.mydomain.com/
and I want the SAME web.config deployed there to work.
Thanks!
I know this question is nearly 4 years old, so you've probably long since moved on from needing an answer.
^ should work though:
<rule name="PageRedirection" enabled="true" stopProcessing="true">
<match url="^urlfolder/filename.aspx" />
<action type="Redirect" url="urlfolder/newfile.aspx" redirectType="Permanent" />
</rule>
^ specifies the beginning of the path string, so whatever you put after the ^ would match only what appears right after the site root and a slash.
e.g.
www.example.com/urlfolder/filename.aspx
would match.
www.example.com/subdirectory/urlfolder/filename.aspx
would NOT match.
The only reason I can think of why it would match /subdirectory/urlfolder/filename.aspx as you say, is that there is a copy of your web.config
in /subdirectory/ in which case www.example.com/subdirectory/ is treated as the root.
If that's the case and your web.config is being copied to places other than your root directory, another option is to add the rule in %windir%\system32\inetsrv\config\ApplicationHost.config instead of a web.config. This is the file that contains all settings that apply to all of IIS on your machine, not just a particular site. In my experience, Visual Studio will refuse to open this file. However, you can edit the file in a different editor, or set the rewrite rules through the IIS GUI.
In the GUI, at the top level of the file tree, the tools available at this level include the URL Rewrite utility, and rules set there write to ApplicationHost.config.
Hope this helps

Creating Url Rewrite to a domain - ASP.NET \ IIS 7

I'm using ASP.NET 3.5 with IIS 7 with the URL Rewrite Module 2.0 installed.
When I create the first test rewrite rule:
<rewrite>
<rules>
<rule name="Test rule1" patternSyntax="ExactMatch">
<match url="w/123/test" />
<action type="Rewrite" url="article.aspx?id=123" />
</rule>
</rules>
</rewrite>
It works perfectly (http://www.myapp.com/w/123/test -> http://www.myapp.com/article.aspx?id=123).
BUT, when I try to use it on a domain that I own, it doesn't work. I assume that something in my syntax is wrong. Here is what i'm trying to do:
<rule name="Test Rule2" patternSyntax="ExactMatch">
<match url="http://www.my-domain.com" />
<action type="Rewrite" url="article.aspx?id=123" />
</rule>
When I try to browse http://www.my-domain.com I expect to be redirected to the article.aspx page, which I don't, I just get 404 page not found.
How should I write a rule for a domain and not for a path ?
Thanks in advance, Gal.
Rules are relative to the place where the web.config lives. You don't need to specify any domain for your rule. The input for your /> is going to be always the URL Path without the query string and without leading slash. That means, if you request "http://www.my-domain.com/" the input is going to be "". If you request "http://www.my-domain.com/w/123/test", the input would be "w/123/test".
If you just browse to http://www.my-domain.com/" the "Default Document" module in IIS will try to rewrite your request to something like http://www.my-domain.com/default.html" and that won't match your rule. Make sure to disable default document module.
If that doesn't work, URL Rewrite has a tracing feature where you can see step-by-step the workflow:
http://learn.iis.net/page.aspx/467/using-failed-request-tracing-to-trace-rewrite-rules/
N.B. For server rules, the input URL includes leading slash always.

Resources