URL Rewrite Module 2.1 redirection http to https not working - iis

They changed the url rewrite Module version (URL Rewrite Module 2.1) and now the redirection from http to https is not working.
Has anyone encountered the same problem?
Application : Angular
System : Windows Server IIS 10
This is the web.config file (it was working for the earliest version of URL rewrite : urlrewrite2.exe)
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<security>
<requestFiltering>
<fileExtensions applyToWebDAV="false">
<add fileExtension=".pdf" allowed="true" />
</fileExtensions>
</requestFiltering>
</security>
<directoryBrowse enabled="true" />
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Credentials" value="true" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" />
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
<rewrite>
<rules>
<rule name="redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Thanks.

For http://example.com:80 to https://example.com:443 your rule posted in the question is correct and working fine on my side.
If it is not working on your side, you could check the Failed Request Tracing logs might give you the information about the issue. The issue might be something else.
For http://example.com:81 to https://example.com:443, you could refer to the rule below.
<rule name="Redirect with port" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(.*):81$" />
</conditions>
<action type="Redirect" url="https://{C:1}:443/{R:0}" />
</rule>
Output:
Let me know if you have further questions.

Related

IIS 10 - JSON files are not serving even after configured MIME and Handlers

I have deployed a web application in IIS 10 (Windows 10) build from Node.
Files structure
I am unable to access the JSON files (for eg., http://localhost:3000/manifest.json) using the direct URL.
Receiving the following error,
I have set MIME Type as follows,
I have tried the Handler Mapping too,
My web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Static Assets" stopProcessing="true">
<match url="([\S]+[.](html|htm|svg|js|css|png|gif|jpg|jpeg|json))" />
<action type="Rewrite" url="/{R:1}" />
</rule>
<rule name="ReactRouter Routes" 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.html" />
</rule>
</rules>
</rewrite>
<handlers>
<add name="StaticFileModuleJson" path="*.json*" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
</handlers>
<directoryBrowse enabled="true" />
</system.webServer>
</configuration>
I notice that your rewrite rule have rewritten .json to .js. It has nothing to do with the handler because static file hanlder is still handling the request.
Please swap the index of js and json.
<rule name="Static Assets" stopProcessing="true">
<match url="([\S]+[.](html|htm|svg|json|css|png|gif|jpg|jpeg|js))" />
<action type="Rewrite" url="/{R:1}" />
</rule>

rewrite rule for folder to different host

There are 2 applications running on local network:
host headers:
http://ui.local, http://api.local
On another server, a website is already setup for url rewriting. Host header of the site is http://externalui.mydomain.com
Here is web.config contents:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://ui.local/{R:1}" />
</rule>
</rules>
</rewrite>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
This url rewriting rule is running without a problem and rewrites all URLs made to http://externalui.mydomain.com to go to http://ui.local
However i want to write another rule so requests made to http://externalui.mydomain.com/api/.... will be forwarded to http://api.local/api/... instead.
How to write a rule for this condition?
You could use the below rule:
<rule name="redirect api" stopProcessing="true">
<match url="api/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="http://api.local/api/{R:1}" />
</rule>
<rule name="all redirect" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
</conditions>
<action type="Redirect" url="http://ui.local/{R:1}" />
</rule>
Note: make sure the API rule should be the first rule.

ARR IIS rewriting websocket (for Home Assistant)

I've tried to set up a ARR internal rewrite for Home Assistant. Home Assistant currently only allows for a single password for logging in and I would like to do this via Windows Forms. So far, so good: the rewrite works, the login works, but I'm getting a 400-exception on the websockets, on which Home Assistant relies heavily.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="http://localhost/(.*)" />
<conditions trackAllCaptures="true">
<add input="{REQUEST_URI}" pattern="^/login(.*)" negate="true" />
</conditions>
<action type="Rewrite" url="{C:1}://192.168.1.11:8123/{R:1}" logRewrittenUrl="true" />
</rule>
<rule name="ReverseProxyInboundRule2" stopProcessing="true">
<match url="ws://localhost/(.*)" />
<conditions>
<add input="{CACHE_URL}" pattern="^ws://" />
</conditions>
<action type="Rewrite" url="ws://192.168.1.11:8123/{R:1}" />
<serverVariables>
<set name="HTTP_SEC_WEBSOCKET_EXTENSIONS" value="0" />
</serverVariables>
</rule>
</rules>
</rewrite>
</system.webServer>
<system.web>
<authentication mode="Forms">
<forms loginUrl="login.aspx" name=".ASPXFORMSAUTH">
</forms>
</authentication>
<authorization>
<deny users="?" />
</authorization>
</system.web>
http works, but ws gives me a 400. Could anyone help me out?
I'm using Windows Server 2012 with latest IIS and also using latest Home Assistant.

Add .html to url in Azure web.config rewrite

I am looking to use the sub-page link for a website on Azure, for example:
Html file mysubpage.html placed in the wwwroot directory in Azure. I wish to be able to access this page by typing mysite.com/mysubpage into the web browser. However, when I visit this url I get the output "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."
I understand that I need to do this with a web.config file in the wwwroot directory, but am unsure as to what contents the web.config file needs to contain?
I currently have the following:
<?xml version="1.0" ?> <configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite Rule">
<match url=".*" />
<conditions>
<add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" />
</rule>
<rule name="Remove html Extension" stopProcessing="true">
<match url="^(.+)\.html$" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="StaticRewrites" defaultValue="">
<add key="/mysubpage" value="/mysubpage.html" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer> </configuration>
Solved the issue. The following worked which removed the removal of the html extension:
<?xml version="1.0" ?> <configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite Rule">
<match url=".*" />
<conditions>
<add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="StaticRewrites" defaultValue="">
<add key="/mysubpage" value="/mysubpage.html" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer> </configuration>
I understand that it you want to re-route numerous urls you can do this by adding extra lines with add as follows:
<add key="/mysubpage1" value="/mysubpage1.html" />
<add key="/mysubpage2" value="/mysubpage2.html" />

IIS URL Rewrite 2.1 skip files that already exist

I have a URL rewrite in my web.config. The rewrite directives are intended to do two things:
If the URL refers to an actual file (such as a css file or image) don't rewrite
If the URL does not refer to an actual file, rewrite to index.php?request={R:1}
Case 2 works perfectly. However, if the requested file exists, I get a generic IIS response indicating an error: HTTP Error 500.50 - URL Rewrite Module Error. - and no other details. The error codes just indicate a generic rewrite module error.
What have I done wrong? This is IIS 10.0
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Do not rewrite existing files and folders" enabled="true" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" />
</conditions>
<action type="None" url="{R:0}" appendQueryString="true" logRewrittenUrl="true" />
</rule>
<rule name="Framework Parsing" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="index.php?request={R:1}" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
<caching>
<profiles>
<remove extension=".php" />
</profiles>
</caching>
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By" />
<add name="Access-Control-Allow-Origin" value="*" />
<add name="X-Content-Type-Options" value="nosniff" />
<add name="X-Xss-Protection" value="1; mode=block" />
<add name="X-Frame-Options" value="SAMEORIGIN" />
<add name="Referrer-Policy" value="origin" />
</customHeaders>
</httpProtocol>
<!-- staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="8.00:00:00" setEtag="true" />
</staticContent -->
</system.webServer>
</configuration>
This is the error page details:
Module RewriteModule
Notification BeginRequest
Handler StaticFile
Error Code 0x80070005
Requested URL XXXXXXXXX/css/foundation/foundation.min.css
Physical Path XXXXXXXXX\public\css\foundation\foundation.min.css
Logon Method Not yet determined
Logon User Not yet determined
I notice that its login method and user is not determined.
Please try to enable anonymous authentication for your rewrite rule. And ensure IUSR have permission to access these files.

Resources