Rewrite rule to map locations to static files - asp.net

What I want to do it rewrite urls for a bunch of static pages in a locations folder such that
/london
maps to the physical file if it exist in the locations folder eg.
/locations/london.aspx
Is this possible with url rewrite. I can't get the rule to work.
<rule name="Rewrite Locations">
<match url="^([_0-9a-z-]+)/*" />
<conditions>
<add input="/locations/{REQUEST_FILENAME}.aspx" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="/locations/{R:1}.aspx" />
</rule>
Many thanks,
Ian

For the IsFile to work you have to pass the right physical path and not the virtual path, you can do something like:
<rule name="Locations" stopProcessing="true">
<match url="^([_0-9a-z-]+)/*" />
<conditions>
<add input="{APPL_PHYSICAL_PATH}locations\{R:1}.aspx" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="/locations/{R:1}.aspx" />
</rule>

Related

redirect url from web.config file not working properly

I want to remove my html extension but failed when going with in the directory.
link1 of my site working properply but other directory links are not working properly.
link2
i want to remove html extension at the end this link
http://mremind.com/products-services/credentialing.html
but after my rewrite
http://mremind.com/products-services/credentialing/
which is not working.
In stackover flow other question i found the solution of my problem but its not working properly.
my solution in web.config file is
<rewrite>
<rules>
<rule name="Hide .html ext">
<match ignoreCase="true" url="^(.*)"/>
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
<add input="{REQUEST_FILENAME}.html" matchType="IsFile"/>
</conditions>
<action type="Rewrite" url="{R:0}.html"/>
</rule>
<rule name="Redirecting .html ext" stopProcessing="true">
<match url="^(.*).html"/>
<conditions logicalGrouping="MatchAny">
<add input="{URL}" pattern="(.*).html"/>
</conditions>
<action type="Redirect" url="{R:1}"/>
</rule>
</rules>
</rewrite>
I think what you want needs another rewrite rule. You redirect from credentialing.html to credentialing, but that file does not exist so you are probably getting a 404.
What you need is a rule that adds the .html extension again as a rewrite so IIS can find the file again.
<rule name="No html ext" stopProcessing="true">
<match url="^([a-z0-9-_/]+)$" ignoreCase="true" />
<action type="Rewrite" url="{R:1}.html" />
</rule>

IIS URL Rewrite for Domains That Share Website

I have two domains both pointing to the same IIS website
domain1.com/website1
domain2.com/website1
I would like to write a URL Rewrite rule to point
domain1.com/*
to a specific subdomain
domain2.com/subfolder.
I can't figure out the IIS URL rewrite rule to accomplish this. Below is what I have:
<rule name="redirect" enabled="true" >stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain1\.com*$" />
</conditions>
<action type="Redirect" url="https://domain2.com/subfolder" />
</rule>
Try this syntax:
<rule name="redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain1\.com*$" />
</conditions>
<action type="Redirect" url="https://domain2.com/subfolder" />
</rule>
i think there is a typo in your code, there is a ">" before stopProcessing that shouldnt be there.

Why isn't this image URL Rewrite rule working?

I have written a URL rewrite to fix images paths to move to a new location for images however it isn't working.
<rule name="Artist Images" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^/images/$" />
</conditions>
<action type="Rewrite" url="https://myserver.blob.core.windows.net/v2/images/" redirectType="Permanent" />
</rule>
The expected behavior is that when a request for an image comes in (http://myserver.com/images/sample.jpg) that the new image location
(https://myserver.blob.core.windows.net/v2/images/sample.jpg) is used.
It is a .net application hosted on Azure. Rule is in the web.config.
Any ideas?
Because your condition is not correct, you are putting sign $ which means end of the string, but this is not correct.
<rule name="Artist Images" stopProcessing="true">
<match url="^images/(.*)$" />
<action type="Rewrite" url="https://myserver.blob.core.windows.net/v2/images/{R:1}" />
</rule>
This is what ended up working but Alexey's answer put us in the right direction.
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_URL}" pattern="/images/(.*)" />
</conditions>
<action type="Redirect" url="https://artistshare.blob.core.windows.net/images/{C:1}" />
</rule>

Setting up URL Rewrite rule for a specific domain SEO

On Google there is the following Url
www.domain.ch/House/rent
So now my page is now available in multi language and i have now the language in the url, and it looks like this
www.domain.ch/en/house/rent
So I will redirect all old links with url rewrite in the web.config, but I can't find out the match condition to find out if there is a languege insite the url.
My role:
<rule name="mydomain.com" >
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(localhost:3005/|localhost:3005/)$" />
</conditions>
<action type="Redirect" url="http://localhost:3005/de/{R:1}" logRewrittenUrl="true" />
</rule>
Thanks for any help!
<rule name="mydomain.com" stopProcessing="true" >
<match url="(.*)" />
<conditions>
<!-- redirect only if the URL doesn't contain /en/ or /de/ already (negate = true)-->
<add input="{URL}" pattern="/en/|/de/" negate="true" />
</conditions>
<action type="Redirect" url="de/{R:1}" />
</rule>

IIS Rewrite root folder to different subfolders

I'm setting up a folder structure inside an application that looks like such:
c:\inetpub\wwwroot\contoso\public
c:\inetpub\wwwroot\contoso\secured
I am wanting to map the following URLs to those folder structures:
http://www.contoso.com/login -> \public\login.aspx
http://www.contoso.com/myaccount -> \secured\myaccount.aspx
http://www.contoso.com/(css|images|js)/* -> \public(css|images|js)* (not represented in below rules)
I have the Application Request Routing Version 2 installed on the server. My thought process was that I could build a few rewrite rules to do the mapping for me such as these ...
<rewrite>
<rules>
<rule name="Rewrite pub page to aspx" stopProcessing="false">
<match url="^([a-z0-9/]+)$" ignoreCase="true" />
<conditions>
<add input="public\{REQUEST_FILENAME}.aspx" matchType="IsFile" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="public/{R:1}.aspx" />
</rule>
<rule name="Rewrite sec page to aspx" stopProcessing="false">
<match url="^([a-z0-9/]+)$" ignoreCase="true" />
<conditions>
<add input="secured\{REQUEST_FILENAME}.aspx" matchType="IsFile" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="secured/{R:1}.aspx" />
</rule>
<rule name="Rewrite 404 page to aspx" stopProcessing="true">
<match url="^([a-z0-9/]+)$" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
</conditions>
<action type="Rewrite" url="public/default.aspx" />
</rule>
</rules>
</rewrite>
<location path="secured"><system.web><authorization><deny users="?"/></authorization></system.web></location>
<location path="public"><system.web><authorization><allow users="?,*"/></authorization></system.web></location>
In my mind, I was telling the condition to check if the file exists in the public folder and if so it would rewrite that file. Otherwise it'd fall through and see if the file exists in the secured folder and if so it would rewrite that file. Otherwise it would get caught by the "catch everything else" rule and just point it back to a default page.
But this is not working to my expectations ... I can get it to always rewrite to a folder but I can't get the conditions to fire to check for a file existing.
Any suggestions?
I turned tracing on within IIS and by looking through those logs I was able to discover that {REQUEST_FILENAME} was the wrong variable to use in this situation. Here's the relevant log information:
Input secured\{REQUEST_FILENAME}.aspx
ExpandedInput secured\c:\inetpub\wwwroot\contoso\myaccount.aspx
MatchType 1
Pattern
Negate false
Succeeded false
MatchType IsFile
So I went looking through the server variables list documentation and was able to find the APPL_PHYSICAL_PATH variable and changed the inputs to this:
<rule name="Rewrite sec page to aspx" stopProcessing="false">
<match url="^([a-z0-9/]+)$" ignoreCase="true" />
<conditions>
<add input="{APPL_PHYSICAL_PATH}secured\{R:1}.aspx" matchType="IsFile" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="secured/{R:1}.aspx" />
</rule>
And voila, that started matching. Hope this helps someone else in the future.

Resources