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

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.

Related

IIS rewrite rule - not working for first level of url pattern

We are trying to add a rewrite rule matching a regular pattern to our wordpress blog using IIS10 (in Windows Server 2019). However the rule is not applying on the first part of the url scheme.
<match url="^blog/(.*)$" />
In this case, the rewrite works for https://example.com/blog/article1 but not for https://example.com/blog/ and is giving us 404 error. Have tried the following with no luck:
Ensure ARR module is installed and Enable proxy is ticked.
Created separate rewrite rule above the existing rule for /blog
<match url="^(blog|blog/)$" />
Created redirect rule for blog to blog/news and then the rewrite rule works. But we would like to preserve the /blog url on the browser.
Here is the rewrite rule that we are trying to crack and would appreciate some suggestions to get it working. Thank you
<rule name="wordpress-rewrite" stopProcessing="true">
<match url="^blog/(.*)$" />
<action type="Rewrite" url="https://blog.example.com/{R:0}" appendQueryString="true" />
</rule>

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 conjunction with server redirect

I'm trying to use URL rewrite with IIS7 to make mydomain.com/home/default.aspx look like mydomain.com. Additionally, the original structure of the site has the root default.aspx redirecting to mydomain.com/home/default.aspx.
Thus, visits to mydomain.com would redirect to mydomain.com/home/default.aspx while showing only mydomain.com in the web browser address bar.
Can someone help me with the Inbound and Outbound rules to make this happen?
Thank you.
Run the iis manager (inetmgr), select your website -> double click on URL rewrite.
You then have to click on the right on Add Rule(s)... and choose Blank Rule.
Fill up the fields with the following values:
This rule will match mydomain.com or mydomain.com/ and rewrite it to mydomain.com/home/default.aspx.
This will produce the following rule in your web.config:
<rules>
<rule name="test" stopProcessing="true">
<match url="^/?$" negate="false" />
<action type="Rewrite" url="home/default.aspx" />
</rule>
</rules>

A very simple IIS7 redirect to different path

I have had a site at www.mysite.com/myfolder/ for years. I have developed a new version and want to just remove the /myfolder so that the site serves all pages from www.mysite.com.
Example: www.mysite.com/myfolder/mypage.htm will be permanently redirected to www.mysite.com/mypage.htm
How do I configure this using the URL Rewrite module in IIS7? I start with a blank rule, but then get lost. Thanks.
You either use the IIS Management UI or edit the web.config directly. With a section in web.config that looks like this, you will redirect everything (images as well), which might be want you want.
<system.webServer>
<rewrite>
<rules>
<rule name="test" stopProcessing="true">
<match url="^myfolder/(.*)" />
<action type="Redirect" url="{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
If you use the Management tool, you add an empty rule, give it a name, add ^myfolder/(.*) as the pattern, select Redirect as action type, add {R:1} as target URL and select 301 as redirect type.
This is supposed to be permanent, that's why you should use a 301 redirect (for SEO purposes and so on).

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

Resources