How to point urls to certain pages with Web.config? - asp.net

I have never written a Web.config file before and to note I cannot use .htaccess. What I am trying to do is fairly easy I believe. I am trying to point a set of URLs to certain pages and/or crossed with setting up vanity URLs. Below are the examples I need:
www.mysite.com/test - this fails to go to www.mysite.com/test/ the slash on the end makes it work. So I want it to work without it.
www.mysite.com/test | get it to work
www.mysite.com/test/index.aspx | goes to | www.mysite.com/test
www.mysite.com/test/page2.aspx | goes to | www.mysite.com/page2 (only the page exist - there is no folder - Like a vanity if possible)
Not sure how to write this, but was trying...
<configuration>
<location path="index.aspx">
<system.webServer>
<httpRedirect enabled="true" destination="www.mysite.com/test" httpResponseStatus="Permanent" />
</system.webServer>
</location>
</configuration>

Sounds like you're looking to rewrite the url. I'm not sue I fully understand what you're after, but if should be pretty simple after looking at a few examples. For instance:
/test/index.aspx -> /test/ could be written as:
<system.webServer>
<rewrite>
<rules>
<!-- /test/index.aspx to /test/ -->
<rule name="test index" stopProcessing="true">
<match url="^test\/index\.aspx$" />
<action type="Rewrite" url="/test/" />
</rule>
</rules>
</rewrite>
</system.webServer>
If you prefer a 301 Permanent Redirect change type="Redirect" instead of Rewrite.
Likewise for specific pages:
<system.webServer>
<rewrite>
<rules>
<!-- /test/page1.aspx to /page1 -->
<rule name="test pages" stopProcessing="true">
<match url="^test/(page\d+)\.aspx$" />
<action type="Rewrite" url="/{R:1}" /> <!-- {r:1} is the "PageN" portion -->
</rule>
</rules>
</rewrite>
</system.webServer>

Related

Redirect Rule in web.config not including folder path

I'm working on a client application where they have a nested HTML-only app in a rooted subfolder of an otherwise ASP.NET application.
The domain name is changing for the HTML app only, and I need to be sure that the requests are routed to the new domain name. So, using the rewriteRules sounds like a perfect tool for this, but I'm doing something wrong and need another set of eyes.
The original URL is something like this:
https://example.com/folder1/folder2/app/index.html
The new domain is on an entirely different server and I want all requests from the app folder to go to the new domain name. Here is an example:
https://newdomain.com/folder1/folder2/app/index.html
Unfortunately, I'm doing something wrong that's probably obvious... I keep getting this URL in the redirect.
https://newdomain.com/index.html
Here is the web.config showing what I tried. It's in the app folder.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<customErrors mode="Off" />
</system.web>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectHtmlApp" stopProcessing="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www\.)?example\.com$" />
</conditions>
<action type="Redirect" url="https://newdomain.com/{R:0}" redirectType="Permanent" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
{R:0} and {R:1} both return index.html.
What is passed to {R:0} is relative. So if your web.config is in /folder1/folder2/app/{here} then "folder1/folder2/app/" are not passed along. If your web.config rule is in the root of the site-app above /folder1, then I am wrong, but if I am right, then you just need to include the path to the root of the app at the destination also. Given what you have above,
<action type="Redirect"
url="https://newdomain.com/folder1/folder2/app/{R:0}"
redirectType="Permanent" appendQueryString="true" />

IIS redirect from subdomain to path

Suppose i have real subdomain like this.
test.example.com, is it possible to write a rule for iis to redirect subdomain to test.example.com/SomeStaticPage.html
the rule should work for multi level subdomains, ie.
test.aaa.bbb.example.com should be redirected to
test.aaa.bbb.example.com/SomeStaticPage.html
Using URL Rewrite is very easy, in fact I'm wondering if I don't get the requirements exactly. (do you need different static pages per domain?, or is it really just to redirect to the same "SomestaticPage.html" while keeping the host name when no default page is specified?)
A simple rule like this would do what your question asks:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectToSomeStatic" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="/SomeStaticPage.html" />
</rule>
</rules>
</rewrite>
<system.webServer>
</configuration>
Now, if you were looking for a more complete solution that allows you to specify a page based on the host name, then you could use a rewrite map where you add the host names that should be redirected and which page they should go to. For example the rule below will check if they are requesting "/" and the host name is in the rewrite map and will use that page to redirect, if it is not in the list then it will not do anything:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectToSomeStatic" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="{C:0}" />
<conditions>
<add input="{HostMap:{HTTP_HOST}}" pattern=".+" />
</conditions>
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="HostMap">
<add key="test.example.com" value="/SomeStaticForTestExample.html" />
<add key="test.a.b.c.example.com" value="/SomePageForTestABC.html" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>

IIS Url Rewrite Rule

I am new to Url rewrite rules so any help would be gratefully appreciated.
Looking to create a IIS Url rewrite that will handle any of the following
http://localhost/homepage/contact-us.aspx -> http://localhost/contact-us.aspx
http://localhost/homepage/about-us.aspx -> http://localhost/about-us.aspx
Any ideas?
I haven't tested this but I think you would have something like this in your web.config file.
You can edit the web.config file and then go into IIS and the rule should show up if you want to test it.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="rewriterule1" enabled="true" stopProcessing="false">
<match url="homepage/(.*)" />
<action type="Rewrite" url="http://localhost/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

IIS 7 URL rewrite module web.config section c#

We are using IIS7 URL rewrite module with asp.net C#, all of the URLs are configured and written directly into web.config:
<system.webServer>
<!-- ... -->
<rewrite>
<rules>
<clear />
<rule name="Category URL" stopProcessing="true">
<match url="somepage.aspx" ignoreCase="false"/>
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{QUERY_STRING}" pattern="REDIRECTION=true"
ignoreCase="false"/>
<add input="{QUERY_STRING}" pattern="categoryid=([^&]*)"/>
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/browsing/categorylanding.aspx?navcategoryid={C:1}"/>
</rule>
</rules>
</rewrite>
</system.webServer>
I need to move this to a section so that I can have seperate rules for QA, DEV and Production, what would be the "type that i would define in the section?
<configSections>
<section name="IISURLRewriteSection" type="???"/>
</configSections>
Will the IIS automatically pick up these settings once moved outside from web.config to another custom config file?
We resorted to writing our own HTTPModule since from debugging point of view as well any issues that would arise at IIS level would have to have the Operations team involved - just following a process defined in our org :)
The best solution, imho, would be to move the configuration details into external files, which you can then swap out per-environment.
This is pretty easy to do using the method described in Moving IIS7 url rewrite section out of the web.config file, where you would have something like this in your Web.config file:
<system.webServer>
<rewrite configSource="rewrite.config"/>
</system.webServer>
Then you can store the environment specific stuff in a separate file, rewrite.config, looking something like this:
<rewrite>
<rules>
<clear />
<rule name="Category URL" stopProcessing="true">
<!-- ... --->
</rule>
</rules>
</rewrite>

URLRewrite IIS 7 Help Needed

Here is my settings for IIS 7 URL Rewrite:
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite to article.aspx">
<match url="^Articles/([0-9]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="articledetails.aspx?articleid={R:1}&title={R:2}" />
</rule>
</rules>
</rewrite>
</system.webServer>
Unfortunately, my page at the following url is not displayed at all:
http://www.highoncoding.com/Articles/723_Introduction_to_IPhone_Development.aspx
Looks like you are giving in the wrong URL.
<match url="^Articles/([0-9]+)/([_0-9a-z-]+)" />
The above rule will match /Articles/723/Introduction_to_Iphone_Development. You are giving it /Articles/723_Introduction_to_Iphone_Development (underscore instead of slash). Seems that you fixed it as I am typing this though :)

Resources