Relative paths not working as expected when using URL Rewrites - html

I'm doing a URL rewrite to a localhost site:
<rule name="test" enabled="true" stopProcessing="true">
<match url="demo/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="http://localhost:7777/{R:1}" logRewrittenUrl="true" />
</rule>
Let's say the folder structure in localhost:7777 is:
/app/something/demo.css
and
/app/index.html
And the call made in the main site is:
http://www.domain.com/afolder/anotherfolder/demo/app/index.html
When I do a simple reference to a css file from index.html which sits in the localhost:7777 website like this:
<link rel="stylesheet" type="text/css" href="/app/something/demo.css">
I would expect the path to be: http://www.domain.com/afolder/anotherfolder/demo/app/something/demo.css.
But instead the path becomes:
http://www.domain.com/app/something/demo.css
Even if I try href="../demo.css" I would expect to see:
http://www.domain.com/afolder/anotherfolder/demo/app/demo.css
But no... I get: http://www.domain.com/afolder/demo.css
Why :( ?????

Related

Web.Config redirect to maintenance page?

From https://gist.github.com/stevensk/c8108311b82ba1591cb1be018bbe0119, I have added the code below to our web.config for "site maintenance page redirect".
For an IP that isn't mine (mine is 3rd item down in IP list), the user is directed to the site maintenance page, but it hasn't been displaying the background image, specified fonts, or other CSS file styles.
The Firefox inspector shows the following message in red:
The resource from
“https://www.example.com/pages/maintenance/message/default.htm”
was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).
How can I get the maintenance page to have the correct fonts, images and other styles applied?
<rewrite>
<!-- Maintenance rewrite map
- Add allowed IP addresses to grant access during maintenance period
- To activate, uncomment Maintenance Redirect rule
- And replace last key with current IP found at /pages/test/remote-name/
-->
<rewriteMaps>
<rewriteMap name="MaintenanceIpAddressWhitelist" defaultValue="">
<add key="192.168.0.1" value="1" />
<add key="127.0.0.1" value="1" />
<add key="198.43.70.125" value="1"/>
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{SERVER_PORT_SECURE}" pattern="^0$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
<rule name="ensurewww" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
</conditions>
<action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" />
</rule>
<!-- Maintenance Rule -->
<rule name="Maintenance Redirect">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{MaintenanceIpAddressWhitelist:{REMOTE_ADDR}}" matchType="Pattern" pattern="1" negate="true" />
<add input="{REQUEST_URI}" pattern="^/pages/maintenance/message/default.htm$" negate="true" />
</conditions>
<action type="Redirect" url="/pages/maintenance/message/default.htm" appendQueryString="false" redirectType="Temporary"/>
</rule>
</rules>
</rewrite>
I ended up embedding images, css, svg and font files as base64 into the default.htm page.

IIS - Remove trailing slash when directory

I want to remove trailing slashes from all requests from "/" to "", even when it's a directory. For instance:
Folder: http://bleh.local/sign-in/
File to be served: http://bleh.local/sign-in/index.html
Default document: index.html
I would like to redirect it to "http://bleh.local/sign-in"
Expected behavior: /sign-in/ redirected to /sign-in
Actual behavior: /sign-in redirected to /sign-in/
Web.config:
<rule name="RemoveTrailingSlashRule1" enabled="true" stopProcessing="true">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}" />
</rule>
applicationHost.config:
<sectionGroup name="rewrite">
<section name="globalRules" overrideModeDefault="Allow" allowDefinition="AppHostOnly" />
<section name="rules" overrideModeDefault="Allow" />
<section name="outboundRules" overrideModeDefault="Allow" />
<section name="providers" overrideModeDefault="Allow" />
<section name="rewriteMaps" overrideModeDefault="Allow" />
<section name="allowedServerVariables" overrideModeDefault="Allow" />
</sectionGroup>
With this setup, I get infinite redirects because I removed this instruction from the original setup:
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
I think my problems come from this: https://support.microsoft.com/en-ca/help/298408/iis-generates-courtesy-redirect-when-folder-without-trailing-slash-is
Any ideas?
Note: How can I configure IIS to serve directories without trailing slashes and always serve relative to root path? is not solving my problem. The backslashes are added consistently.
This is because of the <defaultDocument> in IIS.
<defaultDocument> rule is enabled in IIS by default which forces the page to be redirected to the url with trailing slash, if it did not find a file. domain/sign-in/
<rule name="RemoveTrailingSlashRule1" enabled="true" stopProcessing="true">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}" />
</rule>
Now this rule, matches the url with the trailing slash (and matches directory) and in turn redirects to the one without the trailing slash. domain/sign-in which creates a loop.
The solution is to add the rule<defaultDocument enabled="false"></defaultDocument>.
https://learn.microsoft.com/en-us/iis/configuration/system.webserver/defaultdocument/

Returning a custom response code for for serving static html when using IIS ApplicationInitialization remapManagedRequestsTo feature?

I'm currently using the ApplicationInitialization feature of IIS to warm up my ASP.NET application. I've set the attribute remapManagedRequestsTo to "warmup.html".
<applicationInitialization remapManagedRequestsTo="warmup.html" skipManagedModules="true" doAppInitAfterRestart="true" >
<add initializationPage="/home" />
<add initializationPage="/about-us" />
</applicationInitialization>
It's working well but I would like to return a custom status code when the content for Warmup.html is returned to the browser. This is so that when I run some smoke tests after deployment I get to know when the warm up has finished.
I've tried using URL Rewrite to change the status code from 200 to 555 to serve up warmup.html and it does change the status code but doesn't serve the content in warmup.html
<rewrite>
<rules>
<rule name="Change warm up status code" stopProcessing="true">
<match url="warmup.html" />
<action type="CustomResponse" statusCode="555" subStatusCode="0"/>
</rule>
</rules>
</rewrite>
Is there a way I can do both the serving of warmup.html's content AND return a custom status code of 555?
Finally found my answer in a blog post written by Morten Bock
Turns out I have to remove the two attributes remapManagedRequestsTo and skipManagedModules (default value of false) which leaves us with
<applicationInitialization doAppInitAfterRestart="true">
<add initializationPage="/home" />
<add initializationPage="/about-us" />
</applicationInitialization>
Then let the URL Rewrite module take over but we want to rewrite the response code when the Application Initialization is making the request marked by the server variable APP_WARMING_UP containing a value of 1. When this condition is met we can create a custom response as the action and pop the statusCode attribute with 555.
<rewrite>
<rules>
<rule name="WarmUp" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{APP_WARMING_UP}" pattern="1" />
</conditions>
<action type="CustomResponse" statusCode="555" statusReason="Site is warming up" statusDescription="Try again shortly" />
</rule>
</rules>
</rewrite>
Then to catch the status 555 as a custom error and direct the user to the friendly warm up page warmup.html
<system.webServer>
<httpErrors errorMode="Custom">
<error statusCode="555" path="warmup.html" responseMode="File" />
</httpErrors>
</system.webServer>

Access image through web address without including the extension

I've seen this around the web before but I cannot wrap my mind around how it works. Hopefully someone can help explain this.
Lets say we have a picture at this address. http://www.website.com/image-150x150.jpg
How can I set it up so if you go to http://www.website.com/image-150x150 it shows that picture?
So the only difference is I do not have to include the extension, ie. *.jpg.
On top of all this, I want to be able to refrence it like this as well:
<img src="http://www.website.com/image-150x150" />
If you are using Apache, you need to add a .htaccess rule similiar to this:
RewriteEngine On
redirectMatch 301 ^(.*)\.jpg $1
In the case of IIS (which I haven't used at all!), consider using something like this:
<rewrite>
<rules>
<rule name="rewrite php">
<!--Removes the .php 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" />
<add input="{REQUEST_FILENAME}" negate="true" pattern="(.*).jpg" />
</conditions>
<action type="Rewrite" url="{R:1}.jpg" />
</rule>
</rules>
</rewrite>
I'm sure that you have to change this based on your need.
You can remove the extension and reference it with the img tag and it should work. Since the encoding is of jpeg format and the img tag accepts this encoding, it should be able to convert & display the correct image.

IIS 7 outbound rule not rewriting HTML href

I am trying to rewrite a html response with an outboundrule, but it is not working: This are my inbound and outbound rules:
<rules>
<rule name="IE56 Do not gzip js and css" stopProcessing="false">
<match url="\.(css|js|emz|jpg|htm|html)" />
<action type="None" />
<serverVariables>
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
</rule>
<rule name="Route the requests for eserver" patternSyntax="ECMAScript">
<match url="^eServer/(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{CACHE_URL}" pattern="^(https?)://" />
</conditions>
<action type="Rewrite" url="{C:1}://mckyesvr/eServer/{R:1}" />
<serverVariables>
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
</rule>
<outboundRules>
<rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Area, Base, Form, Frame, Head, IFrame,Img, Input, Link, Script" pattern="^http(s)?://mckyesvr/eServer/(.*)" ignoreCase="true" />
<conditions>
<add input="{URL}" pattern="/PageLoader" negate="true" />
</conditions>
<action type="Rewrite" value="/eServer/{R:2}" />
</rule>
<rule name="ReverseProxyOutboundRule8" preCondition="ResponseIsHtml1">
<match pattern="http://mckyesvr/eServer/PageLoader.asp?Page=Process_Safety.dsp"/>
<action type="Rewrite" value="/eServer/PageLoader.asp?Page=Process_Safety.dsp" />
</rule>
They work perfectly with almost every HTML response except with the ones that have this HTML reponses form:
"<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
.......etc and this is the part that I want to rewrite:
<_a href="http://mckyesvr/eServer/PageLoader.asp?Page=Process_Safety.dsp"
target="_parent">Go Back to Process Safety------
I have tried changing the compressing, change the outbound rules, move a thousand parameters but nothing has worked out until now, I don't know why this is happening just with an HTML response as the one indicated above, it looks like it is coded because Outboundrules don't catch it.