IIS URL Rewrite redirect all subdirectories - asp.net

My website has a lot of old pages and directories not in use. Instead of deleting them, I'm thinking about using the rewrite rules to redirect them to its parent directory,
For example, I have these links:
https://www.example.com/category/sub/1
https://www.example.com/category/sub2222/1/a
https://www.example.com/category/something/sub/3333
I want all three of them to redirect to https://www.example.com/category.
I tried the code below but got into an infinite loop. Guess I have to specify the rule do redirect ONLY when it has a subdirectory:
<rule name="Redirect-test" stopProcessing="true">
<match url="category/(.*)" ignoreCase="true" />
<action type="Redirect" url="https://www.example.com/category" />
</rule>

ok, maybe I was overthinking of this, adding a /(.*) would solve my problem, I'm just wondering if there's any flaws in such rule
<rule name="Redirect-test" stopProcessing="true">
<match url="category/(.*)/(.*)" ignoreCase="true" />
<action type="Redirect" url="https://www.example.com/category" />
</rule>

Related

Ensure www using webconfig rewrite

I'm working with an Umbraco asp.net website and I would like to ensure website url is alwayes displayed with www I have added:
<rule name="WWW rule" stopProcessing="false">
<match url=".*" />
<conditions>
<add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
</conditions>
<action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" />
</rule>
To me this is a good methode for making sure only one URL on website. Only issue as I see is that when typing DOMAINE only my default.aspx will be added to frontpage.
How to avoid that?
I have tried to add:
<rule name="Default Document" stopProcessing="true">
<match url="(.*)default.aspx" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
This makes frontpage inside a loop....
Have I misunderstood somthing? Thanks
Shouldn't stopProcessing="true" for the domain rewrite? I'm guessing you don't want the rest of the rules evaluated?
Also, I'm guessing you might want to use {HTTP_HOST} in you condition, since I would guess that the {CACHE_URL} would be the one including the default.aspx path?
Currently you are using the incoming http/https schema, which are also considered different url's, so you may want to choose which one to use, or implement a canonical meta tag on your site to tell search engines which schema to use.

Url rewrite rule for redirecting to specific page in asp.net webforms

ASP.NET 4.5 Webforms website
I have two domains:
company.com
company.co.ca
Both pointing to the same site and the default document is set to login.aspx.
When someone goes to company.co.ca, I check the Request.ServerVariables["HTTP_HOST"] in the login.aspx.cs and redirect users to company.co.ca/ca/login.aspx
I thought I can use the url rewrite and have this rule in my web.config but it doesn't redirect but stays on the main login.aspx.
<rewrite>
<rules>
<rule name="CA HomePage" stopProcessing="true">
<match url="company.co.ca/login.aspx" />
<action type="Redirect" url="company.co.ca/ca/login.aspx" />
</rule>
</rules>
</rewrite>
What am I doing wrong? Is there a better way?
We are planning to add one more domain and so I don't want to do a manual check in login.aspx.cs and redirect and want to find a more general best way.
You check for the host like this with IIS rewrites:
<rule name="CA HomePage" stopProcessing="true">
<match url="^login.aspx$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^company.com$" />
</conditions>
<action type="Redirect" url="http://company.co.ca/ca/login.aspx" />
</rule>

IIS 7 Url Rewrite - Matching Sub Domains, but excluding specific urls

I have a URL rewrite set up on a site where users can create their own sites on a subdomain.
<rule name="CName to URL - Rewrite" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www)(.*)\.basedomain\.com" />
</conditions>
<action type="Rewrite" url="render_page.aspx?site={C:1}&page={R:0}" />
</rule>
As an example, a user could create a page http://client.basedomain.com/about-us and this will successfully pass "client" and "about-us" to my application.
What I need to be able to do is override this behavior in the case of a specific page name following their domain.
So if the page http://client.basedomain.com/restricted was accessed, it would not perform the above rewrite, and instead rewrite it to the url "render_page_restricted.aspx?site={C:1}
Any help is GREATLY appreciated.
In standard fashion, after spending a few days working on it, then finally posting it on SE, I cracked it 20 min later. Sigh. In the event someone else has the same problem, here's my fix:
<rule name="Restricted Page Name Override - Rewrite" stopProcessing="true">
<match url="^(?!www)(.*)restricted" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www)(.*)\.basedomain\.com"/>
</conditions>
<action type="Rewrite" url="render_page_restricted.asp?site={C:1}" />
</rule>

IIS7 Rewrite issue - Multiple websites

Hi have an issue with the rewrite mod for IIS7
I have several websites in this example (seasons.com. summer.com, winter.com, spring.com)
All of these domains point to the same folder (g:\websites\seasons\htdocs)
Now the complicated bit.
Summer.com gets rewritten to g:\websites\seasons\htdocs\domains\summer\ so any file in this folder would appear like http://www.summer.com/helloworld.php. This rewrite appears in the g:\websites\seasons\htdocs\web.config
Now summer.com also has its own rewrite rules contained in a web.config file located in g:\websites\seasons\htdocs\domains\summer\web.config
If a visitor attempts to visit http://summer.com/news/
the rewrite moudle rewrites should rewrite to the news.php file contained within the summer folder (g:\websites\seasons\htdocs\domains\summer\news.php) but instead it rewrites to
url: http://www.summer.com:80/domains/summer/news/
path: g:\websites\seasons\htdocs\domains\summer\news\
Here are the rewrite rules
The file located at g:\websites\seasons\htdocs\web.config
<rule name="summer">
<match url="(.*)" />
conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="summer.com" />
</conditions>
<action type="Rewrite" url="/domains/summer/{R:0}" />
</rule>
the file located at g:\websites\seasons\htdcoc\domains\summer\web.config
<rule name="Rewrite to news">
<match url="^news/" />
<action type="Rewrite" url="news.php" />
</rule>
I know this isn't perfect. Any help appreciated

Redirect from Default.aspx to root using IIS7

When request comes to www.example.com/default.aspx, I want it to 301 to www.example.com. How to do that?
PS - I tried many rules but nothing seems to work. Thanks.
Have you tried using URL Rewrite with the following rule:
<rule name="Default Document" stopProcessing="true">
<match url="(.*)default.aspx" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>

Resources