Asp.net allow access only through routes? - asp.net

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.

Related

ASP.net URL Rewrite subdirectory to external URL

I need to URL Rewrite a subdirectory to an external domain.
Example: When visiting "https://example1.com/test", "https://example2.com/hello" should open. The URL should still be "https://example1.com/test".
I tried to solve this by adding a Rewrite rule in web.config, but it don't works. Here is the Rewrite rule I made:
<rule name="TestRule" stopProcessing="true">
<match url="^test(/.*)?$" />
<action type="Rewrite" url="https://example2.com/hello" appendQueryString="false" />
</rule>
In order to redirect the incoming request to another domain by using Rewrite action type (stay the old URL in the browser address bar), we need to install Application Request Routing module.
https://www.iis.net/downloads/microsoft/application-request-routing
By default, Rewrite action only forwards these requests to the same domain, therefore, we only can specify a URL path in the Rewrite URL field.
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#rewrite-action
Otherwise, Redirecting the incoming request to another domain will cause a 404 error.
After we installed the ARR extension, enable Reverse Proxy functionality following the below steps.
Here is an official example.
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/reverse-proxy-with-url-rewrite-v2-and-application-request-routing
Finally, please refer to the below configuration.
<rewrite>
<rules>
<rule name="Myrules" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{PATH_INFO}" pattern="/test.*" />
</conditions>
<action type="Rewrite" url="https://www.bing.com/maps" appendQueryString="false" logRewrittenUrl="false" />
</rule>
</rules>
</rewrite>
It will redirect the requests that has “/test” segment to the https://www.bing.com/maps.
If we want to complete the task without installing ARR extension, we have to use Redirect action type. The URL in the browser address bar will change while we access the qualified URL.
<rewrite>
<rules>
<rule name="Myrules" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{PATH_INFO}" pattern="/test.*" />
</conditions>
<action type="Redirect" url="https://www.bing.com/maps" appendQueryString="false" logRewrittenUrl="false" />
</rule>
</rules>
</rewrite>
Feel free to let me know if there is anything I can help with.

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.0 - URL Rewriting Root Folder

I have done a bunch of sites using URL Rewrite actions in the web.config with much success.
Today I am trying to do the following rewrite rule:
Example url: domainname.com/ANYTHINGHERE/PRODUCTID
I basically want to take the product ID and pass it to a page (i.e. product.aspx?id=PRODUCTID
<rule name="Rewrite Product Details Page" stopProcessing="true">
<match url="^([^/]+)/([^/]+)$" />
<action type="Rewrite" url="/product-view.aspx?id={R:1}" appendQueryString="true" logRewrittenUrl="true" />
</rule>
I've done this in the past just fine but it had to have a static root folder, for instance: domanname.com/products/ANYTHINGHERE/ID
EDIT: (forgot to describe my issue),
When I try to load my page - domain.com/categoryname/PRODUCTID
The product page is called by the ID is not passed in.
Any ideas? I searched and could not find anything.
-Joe
I think the index is not zero-based. Try
<rule name="Rewrite Product Details Page" stopProcessing="true">
<match url="^([^/]+)/([^/]+)$" />
<action type="Rewrite" url="/product-view.aspx?id={R:2}" appendQueryString="true" logRewrittenUrl="true" />
</rule>
Here is a good tutorial: Creating Rewrite Rules for the URL Rewrite Module

Rewrite Subfolder to Subdomain in web.config

I'm attempting to write a rewrite rule for the following scenario.
User attempts to load this picture:
domain.com/images/folder/picture.jpg
and instead, I need it to load:
cdn.domain.com/images/folder/picture.jpg.
Here's what I have that isn't working:
<rule name="CDN rewrite for Images">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="domain.com" />
<add input="{REQUEST_URI}" pattern="^/images/folder/(.*)$" />
</conditions>
<action type="Rewrite" url="cdn.domain.com/images/folder/{C:1}" />
</rule>
UPDATE: Adding additional info. Most pictures are being served up from Joomla so while the root of the domain is something like domain.com, most images are input with a src="/images/folder/picture.jpg" Not quite sure how this is affecting the rewrite, but none of the options on cheesemacfly's answer below, are working...
UPDATE2: While cheesemacfly was unable to help me in my particular circumstances, I awarded him the bounty and marked his answer as the accepted one because he went above and beyond to try to help me in chat. Hopefully his answer will help someone with rewrites on IIS.
EDIT:
To be able to rewrite (and not only redirect) urls to outside websites, you need to install the Application Request Routing module and enable the proxy mode.
To do so:
Download and install the module
Open your IIS management console (inetmgr)
Select Your server node
Double click on Application Request Routing Cache:
Click on Server Proxy Settings on the Actions pane (right of the screen)
Check the box Enable proxy and click on Apply
The second step is about setting up your rules.
If you want your rewrite to be based on the path then use the following code:
<rewrite>
<rules>
<rule name="Rewrite to cdn domain">
<match url="^images/folder/(.+)$" />
<action type="Rewrite" url="http://cdn.domain.com/images/folder/{R:1}" />
</rule>
</rules>
</rewrite>
Or if you keep the same folder architecture on the second website you can simplify as follow:
<rewrite>
<rules>
<rule name="Rewrite to cdn domain">
<match url="^images/folder/(.+)$" />
<action type="Rewrite" url="http://cdn.domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
If you want to catch only the files ending with a specific extension (let's say images):
<rewrite>
<rules>
<rule name="Forward to cdn domain">
<match url="^images/folder/.+\.(?:jpg|bmp|gif)$" />
<action type="Rewrite" url="http://cdn.domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
Please refer to: http://www.iis.net/learn/extensions/url-rewrite-module/iis-url-rewriting-and-aspnet-routing (section "Which Option Should You Use?")
TIP:
The best way to test your pattern is to use the IIS test pattern tool.
At the root of your website -> URL Rewrite -> Create a blank rule -> click on test pattern:
If you don't get the expected result, you can debug your rewrite using the Failed Request Tracing tool
NOTE: Changing the rule to be a redirect instead of a rewrite fixes the problem. Ideally you want it to be a redirect but I have spent many hours trying to get the rewrite to work, and so far no solutions yet.
<rule name="Rewrite to images.cdn.com" enabled="true" stopProcessing="true">
<match url="images/(.+)$" ignoreCase="true" />
<action type="Redirect" url="http://images.cdn.com/{R:1}" />
</rule>

Resources