IIS - Remove trailing slash when directory - html

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/

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.

We need to force "http" for a single url . We already have a redirect of all urls to https

Our site https://www.globalcompanyformation.co.uk/ has a redirect to "https://" in the web.config file for all urls .
Now we have a form which sends information to our subdomain which is hosted in another ip address and which is not secure . This form is in our page https://www.globalcompanyformation.co.uk/Gcf-index.html . Firefox gives a warning message that the site to which you are redirected is not secure . We dont want to see this alert and when i checked whole internet the only solution was that i get the page "Gcf-index.html" to have "http" and not "https" .
Is there any way that i could force Gcf-index.html to open in "http" . The web.config coding is as follows as of now for redirecting to https for all urls .
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{REQUEST_FILENAME}" negate="true" pattern="^Gcf-index.*" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
Change the second condition to
<add input="{REQUEST_FILENAME}" negate="true" pattern="Gcf-index\.html" />
Add the following rule to force http on that form page.
<rule name="Force http on GCF" stopProcessing="true">
<match url="Gcf-index\.html$" />
<conditions>
<add input="{HTTPS}" pattern="on" ignoreCase="true" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}{REQUEST_URI}"
redirectType="Permanent" appendQueryString="false" />
</rule>

hide/remove url extension in windows servers

I read that .htaccess doesn't work on windows server like godaddy. So to hide or remove extensions like .php and .html on URL, you can accomplish this by creating a web.config file with the following code:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RewriteHTML">
<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}.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I tried to make web.config file and save it where index.php is with that code but nothing happened.. I tested it with the like this
<ul>
<li>
Attack on Titan Episode 3
</li>
</ul> with a href of "attackontitan-3"
I want url to be http://mysite/watching/attack%20on%20titan/attackontitan-3 not http://mysite/watching/attack%20on%20titan/attackontitan-3.php
Use rewrite to add the extension, like this:
<rule name="rewritephp">
<!--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}.php" />
</rule>
Or This tutorial does a great job at explaining what you need to do with IIS.
http://atlantawebsites.blogspot.com/2010/06/vanity-urls-with-godaddy-hosting-using.html
You need to add a Rewrite and Redirect rules. The following will redirect and rewrite page.php to just page. If you want to do the same for html, just add those rules.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<!-- Remove the existing rules just incase we already defined them -->
<remove name="RedirectPhpExtensions" />
<remove name="RewritePhpExtensions" />
<!-- Add a rule to redirect x.php pages to just x -->
<rule name="RedirectPhpExtensions" enabled="true" stopProcessing="true">
<match url="(.*)\.php$" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
<!-- Add a rule to rewrite the x pages to x.php behind the scene -->
<rule name="RewritePhpExtensions" enabled="true" stopProcessing="true">
<match url="(.*)" negate="false" />
<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}.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Place this web.config in the root directory of your domain.
Then you need to restart the Application Pool on godaddy's site:
Web Hosting > Manage > IIS Management > Recycle App Pool button

Exceptions in redirection in URL in IIS Rewrite module are ignored

I have a new site https://www.NewSite.com and I would like all the traffic to my old site https://www.OldSite.com to be redirected to the root of my new site https://www.NewSite.com/, with some exception.
For instance I would like the URL (among others) https://www.OldSite.com/noredirect not to be redirected and others to be rewritten. For instance:
https://www.OldSite.com -> https://www.NewSite.com/
https://www.OldSite.com/library->https://www.NewSite.com/
https://www.OldSite.com/kb/ -> https://www.newsite.com/kb
https://www.OldSite.com/kb/articles.aspx?id=45 -> https://www.NewSite.com/kb
https://www.OldSite.com/noredirect -> https://www.oldsite.com/noredirect
https://www.OldSite.com/noredirect/page.aspx -> https://www.oldsite.com/noredirect/page.aspx
Therefore I wrote two rules in my Old Site site in IIS:
<rewrite>
<rules>
<clear />
<rule name="Rewrite To New Site" stopProcessing="true">
<match url="/kb(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="https://www.newsite.com/kb" />
</rule>
<rule name="Redirect to New Site" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_URI}" pattern="(.*)/noredirect$" negate="true" />
</conditions>
<action type="Redirect" url="https://www.newsite.com" appendQueryString="false" />
</rule>
</rules>
</rewrite>
This rules just redirect all the requests to my old site to the root of my new site, ignoring the exceptions and the rewrite rule. The same happens with:
<rule name="Redirect to New Site" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_URI}" pattern="^/noredirect(.*)" negate="true" />
</conditions>
<action type="Redirect" url="https://www.newsite.com" appendQueryString="false" />
</rule>
What am I doing wrong?
Make sure you've checked "Enable proxy" under Application Request Routing cache.
You can find some information about it here:
http://www.iis.net/learn/extensions/configuring-application-request-routing-%28arr%29/creating-a-forward-proxy-using-application-request-routing
Good luck!

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.