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

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. :)

Related

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?

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?

Wordpress - IIS 7.5 - URL rewrite - OWA and RWW 403.18 Forbidden

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Main Rule" 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="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
This rule is located in my wwwroot folder, it fixes my URL's, but it breaks my OWA and RWW websites.
How can I add an exception, or condition, to this rule so that it ignores a particular address? I am trying to process the domain name remote.mydomain.com, normally.
With the rule above in place, remote.mydomain.com, will return a 403 error.
You can simply add a condition to your rule:
<rule name="Main Rule" 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="{HTTP_HOST}" pattern="^remote\.mydomain\.com$" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
the condition
<add input="{HTTP_HOST}" pattern="^remote\.mydomain\.com$" negate="true" />`
will prevent your rule from trigger if the requested host is exactly remote.mydomain.com

Wordpress blog inside another [permalinks]

I have a question about having two blogs at the same host simultaneously. Here is my scenario:
The first blog is hosted in the root directory http://mathosproject.com/, and the second is hosted in a sub directory, http://blog.mathosproject.com/, which is linked to http://mathosproject.com/blog/.
I have tried to manipulate the web.config file of the root, by making it excluding the directory blog as following:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="wordpress8" patternSyntax="Wildcard">
<match url="^(blog|sample-page)*" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(blog|index)" negate="true" />
</conditions>
<action type="Rewrite" url="blog/index.php" />
</rule>
<rule name="wordpress" patternSyntax="Wildcard">
<match url="*" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(blog|index)" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
This actually helped me to be able to view the second blog, in blog, but as I processed to the sample-page, I was redirected to the first blog.
My question is, is there a way I can configure the web.config, so that it excludes a folder entirely?
Thank you in advance,
Artem
I guess this is so late but have answer so posting it.
In subdirectory blog, edit the web.config and put <Clear/> just after <rules> element.
That will make your both the blogs working fine.

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>

Resources