Remove ASPX FILE Extention in Web.config But Negate Rewrite if URL Contains Period - asp.net

All right, so by now most of us probably use the standard rule below for remove the aspx extention in your urls.
<rule name="Remove">
<!--Removes the .aspx extension for all pages.-->
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.aspx" />
</rule>
However I would like to modify the rule to prevent the write rule from catching any url with a period in it.
That way if someone tries to type in http://www.domain.com/picture.jpg
The rule doesn't catch it.
Luckily the IsFile and IsDirectory conditions prevent actual files from being hit by the rule but whenever I have a case where someone types in a file that doesn't exist on the server then the rule catches it and asp.net does something like this:
http://www.domain.com/404error.aspx?aspxerrorpath=/picture.jpg.aspx
Id like it to not pass through the rule when there is a file not found.
Basically I just need to be able to add a condition that negates whenever a period is found in the url after the domain name. I'm assuming some sort of REGEX will work. I can't seem to get it working right though.

I was able to come up with the following solution.
<rule name="RewriteASPX" stopProcessing="true" enabled="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}.aspx" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:1}.aspx" />
</rule>

Related

Redirect to subdomain using web.config rules

we migrate our webapp hosted in a directory "webapp" inside main domain:
http://www.example.com/webapp/
To the new subdomain without "webapp" folder:
http://subdomain.example.com/
I need to redirect only subdir "webapp". I don't want to redirect other url like http://www.example.com/, http://www.example.com/folder1, http://www.example.com/folder2, ....
But we have already sent a lot of emails to customers with link inside like: LOGIN TO YOUR DASHBOARD or other link like DOWNLOAD YOUR DOC
Now i'm trying to do a redirect trough web.config but it seem that nothing work.
If I try no navigate to, for example, http://www.example.com/webapp/login i don't get redirected to http://subdomain.example.com/login/ as expected.
If I try no navigate to, for example, http://www.example.com/webapp/data/download.php/id=43 i don't get redirected to http://subdomain.example.com/data/download.php/id=43 as expected.
Any suggest?
<rule name="redirect-to-subdomain" stopProcessing="true">
<match url="^webapp/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" url="http://subdomain.example.com/{R:0}" redirectType="Permanent" />
</rule>
You rule won't work except for http://www.example.com/webapp/.
If you want to redirect link like http://www.example.com/webapp/login. Please try this rule.
<rule name="redirect-to-subdomain" stopProcessing="true">
<match url="^webapp/(.*)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" url="http://subdomain.example.com/{R:1}" redirectType="Temporary" />
</rule>

Rewrite Rule In ASP.NET

I use a rewrite rule in IIS in single page app and using angular ui router to serve my project but when I send request to api service it not call it and redirect to the page that I put in the action of the rule and the response return me with HTML page in string format that is the Single Page Body returned to me instead of the expected data that I want from consuming the service.
<rule name="HomeRule" stopProcessing="true">
<match url="po" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/Pages/MasterPage.html" />
</rule>
please any one help to fix this issue
thanks in advance
Take a look at what this is saying.
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
With MatchAll logical grouping, its saying only execute the rule if the filename is not a directory and not a filename. Is that what you really want?

IIS rewrite - add '.html' to any URL's that don't have an extension

I'm trying to setup an IIS rewrite rule to add '.html' to URL's that don't have an extension, eg:
Original URL: www.domain.com/page
Rewrite to: www.domain.com/page.html
And I want to ignore any URL's that have an extension (eg. if they are images or other files)
Does anybody know the rule that I would need to set this up?
I worked this out myself by modifying a rule that I had on another website:
<rule name="rewrite directories to html" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(.*?)\.[a-zA-Z]{1,4}$" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.html" />
</rule>

IIS URL Rewrite: Add trailing slash except for .html and .aspx

Adding a trailing slash to all URLs through IIS URL Rewrite Module is widely spread, but how do I add exceptions for URLs that ends with .html and .aspx?
Today I have this:
<rule name="Add trailing slash" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<!-- Doesn't seem to be working -->
<!--<add input="{REQUEST_URI}" pattern="(.*?).html$" negate="true" />-->
<!--<add input="{REQUEST_URI}" pattern="(.*?).aspx$" negate="true" />-->
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule>
If you want something done right, you've got to do it yourself, obviously...
Here is the solution to my question:
<rule name="Add trailing slash" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(.*?)\.html$" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(.*?)\.aspx$" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule>
Update: I blogged about this in more detail.
Varying the other answers, I used this so I wouldn't have to specify a list of file extensions:
<!-- Ensure trailing slash -->
<rule name="Add trailing slash" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(.*?)\.[a-zA-Z]{1,4}$" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule>
We add multiple extensions like this:
<add input="{URL}" negate="true" pattern="((.+).(jpg|ico|gif|js|png|htm|css|html))" ignoreCase="true" />
To prevent all files from having a slash added, I changed the match rule to this:
<match url="^([^.]*[^/])$" />
That applies the rule only to paths that include any number of non-dot characters that does not end in a slash. So any path that includes a dot (e.g. xxx.html, xxx.aspx, etc.) would be excluded without needing any additional negation rule.
Looking for the presence of a dot in the match rule allowed me to completely remove the condition rules that use match types IsFile and IsDirectory. Those match types are only allowed in distributed rules (web.config), not in the global rules (applicationHost.config), so I had been forced to replicate this rule for every site instead of applying it to all sites using a global rule. By modifying the regex in the match rule to exclude files and removing the IsFile and IsDirectory conditions, I was able to create a global rule instead of having multiple distributed rules.
<conditions>
<add input="{URL}" pattern="(.*)\.(.*)[a-z]$" negate="true" />
<add input="{URL}" pattern="(.*)\.(.*)[0-9]$" negate="true" />
</conditions>
except for .html and .aspx and .woff2
This almost worked for me. I had to change it to
<add input="{URL}" pattern="(.*?)\.html$" negate="true" />
<add input="{URL}" pattern="(.*?)\.aspx$" negate="true" />
Otherwise thanks for this!
To prevent POST, DELETE and other REST method calls without a trailing slash from erroneously becoming a GET request through the redirect consider adding the following condition:
<add input="{REQUEST_METHOD}" matchType="Pattern" pattern="GET" ignoreCase="true" />
You can try this:
<conditions>
<add input="{URL}" pattern="(.*)\.(.*)$" negate="true" />
</conditions>

Adding ignore case in URL rewriting in ASP.Net

I am using following web.config entry for url rewriting to remove .aspx extension.
<rewrite url="~/(.+)" to="~/$1.aspx" />
The problem I am getting here is if I have any image on page, it assigns .aspx extension to image.
Also if I tried to access my site like http://exmaple.com, it get redirected to http://exmaple.com/default.aspx.aspx.
I want to know if there is any way to add ignore case in web.config.
Your rewrite should look something like this, to remove .aspx
<rewrite>
<rules>
<rule name="RewriteASPX">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.aspx" />
</rule>
</rules>
</rewrite>

Resources