Rewrite Rule In ASP.NET - 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?

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>

Scriptmanager issue because of rewrite rule in web.config for angularjs

I have an angulajs application which implements routing
as
.config(function ($routeProvider, $locationProvider) {
$routeProvider
.when("/home", {
templateUrl: "home.html",
controller: "homeController"
})
.when("/filter", {
templateUrl: "filter.html",
controller: "filterController"
})
and i have a rewrite rule in asp.net web.config as follows
as suggested here--SO
<rewrite>
<rules>
<rule name="AngularJS" stopProcessing="true">
<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="/webmasterpage.html" />
</rule>
</rules>
</rewrite>
to make routing work in asp application, but because of the rewrite rule i am facing
Unexpected token < error in WebResource.axd and ScriptResource.axd
and also
ASP.NET Ajax client-side framework failed to load error for
asp:scriptmanager
if i comment out rewrite rule in web.config file this error goes away but angularjs routing wont work! In what way we could write the rewrite rule so that we get both (asp scriptmanager and angularjs routing) working fine. any help is appreciated
So after googling a lot finally i was able to break through the same. sharing my answer here so it could help somebody in future.
so i configured the rules for angular in web.config file as follows :
<rewrite>
<rules>
<rule name="AngularJS" stopProcessing="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}" pattern="(.*?)\.html$" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(.*?)\.js$" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(.*?)\.css$" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(.*?)\.aspx$" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(.*?)\.axd$" negate="true" />
</conditions>
<action type="Rewrite" url="/webmasterpage.html" />
</rule>
</rules>
</rewrite>
you can note here
<add input="{REQUEST_FILENAME}" pattern="(.*?)\.html$" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(.*?)\.js$" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(.*?)\.css$" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(.*?)\.aspx$" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="(.*?)\.axd$" negate="true" />
I have specifically mentioned file extensions which I want to load and get accessed exclusively. the .axd was getting generated on the fly by asp and was not getting loaded, so after getting listed specifically in rules it is now getting loaded.
I don't know much technicalities but this is what worked for me and i am sure will help somebody else too. :)

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>

Conflicting URL rewrite rules

I have two URL rewrite rules. Individually they work perfectly, but when both are turned on, they are conflicting.
The first is in my root dir web.config, takes all requests to files in /seo/ and serves them by requests in the root of the website.
<rule name="RewriteToFile">
<match url="^(?!seo/)(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="seo/{R:1}" />
</rule>
The next one is in the web.config in a sub directory called /blog (it's a Wordpress blog).
<rule name="wordpress" patternSyntax="Wildcard">
<match url="*"/>
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
</conditions>
<action type="Rewrite" url="index.php"/>
</rule>
Individually turned on, they work fine, but if both of them are on, the blog will only serve 403's...
Is there a way to enable both of these rules?

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

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>

Resources