I have created website with React project and hosted on IIS webserver(ON VM). I have defined web.config file rules
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
<iisnode node_env="%node_env%" nodeProcessCountPerApplication="1" maxConcurrentRequestsPerProcess="1024" maxNamedPipeConnectionRetry="100" namedPipeConnectionRetryDelay="250" maxNamedPipeConnectionPoolSize="512" maxNamedPipePooledConnectionAge="30000" asyncCompletionThreadCount="0" initialRequestBufferSize="4096" maxRequestBufferSize="65536" uncFileChangesPollingInterval="5000" gracefulShutdownTimeout="60000" loggingEnabled="true" logDirectory="iisnode" debuggingEnabled="true" debugHeaderEnabled="false" debuggerPortRange="5058-6058" debuggerPathSegment="debug" maxLogFileSizeInKB="128" maxTotalLogFileSizeInKB="1024" maxLogFiles="20" devErrorsEnabled="true" flushResponse="false" enableXFF="false" promoteServerVars="" configOverrides="iisnode.yml" watchedFiles="web.config;*.js" nodeProcessCommandLine="C:\Program Files\nodejs\node.exe" />
<handlers>
<add name="iisnode" path="server.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<rule name="Redirect HTTP to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="non-wwwtowww" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.abc$" />
</conditions>
<action type="Redirect" url="https://www.example.abc/{R:1}" />
</rule>
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="server.js"/>
</rule>
</rules>
<outboundRules>
<rule name="Add the STS header in HTTPS responses">
<match serverVariable="RESPONSE_Strict_Transport_Security" pattern=".*" />
<conditions>
</conditions>
<action type="Rewrite" value="max-age=31536000" />
</rule>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>
The problem with the above rules is when I want to open my website directly its redirects to https://www.example.abc/server.js. I want to open https://www.example.abc .
The reason for redirecting to https://www.example.abc/server.js is because of your DynamicContent rule, if you want to redirect subdomain to main domain, you can refer to this rule:
<rule name="test">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.abc$" />
</conditions>
<action type="Redirect" url="https://www.example.abc" />
</rule>
Related
I have created reverse proxy Windows server 2016 and IIS.
Everything is working fine, but the problem is following it doesnt allow the url like this,
https://mywebsite/shop/product/3231?utm_source=IGShopping&utm_medium=Social
but when I replace utm_source as utm_sour or something then it works fine.
In simple words, with utm_source and utm_medium variables it gives this error.
Screenshot of the error
Directly with the localhost it works fine.
https://mywebsite/shop/product/3231?utm_source=IGShopping&utm_medium=Social
For the reference, my web.config file is as follows
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<outboundRules>
<clear />
<rule name="Add SameSite" preCondition="No SameSite">
<match serverVariable="RESPONSE_Set_Cookie" pattern=".*" negate="false" />
<action type="Rewrite" value="{R:0}; SameSite=None; Secure" />
</rule>
<rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1" enabled="true">
<match filterByTags="A, Form, Img" pattern="^http(s)?://localhost:8068/(.*)" />
<action type="Rewrite" value="http{R:1}://mocauae.ae/{R:2}" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
<preCondition name="No SameSite">
<add input="{RESPONSE_Set_Cookie}" pattern="." />
<add input="{RESPONSE_Set_Cookie}" pattern="; SameSite=None; Secure" negate="true" />
</preCondition>
</preConditions>
</outboundRules>
<rules>
<rule name="ReverseProxyInboundRule1" enabled="true" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://localhost:8068/{R:1}" />
</rule>
<rule name="HTTPS" enabled="false" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="http://localhost:8068" redirectType="Found" />
</rule>
<rule name="HTTPS force" enabled="false" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://localhost:8068" appendQueryString="false" redirectType="Permanent" />
</rule>
<rule name="https pure" enabled="false" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<action type="Redirect" url="https://localhost:8068" redirectType="Found" />
<conditions>
<add input="{HTTP}" pattern="off" />
</conditions>
</rule>
</rules>
</rewrite>
<httpRedirect enabled="false" destination="http://localhost:8068" exactDestination="true" />
</system.webServer>
</configuration>
I the solution by just simply ignoring the url parameter and it worked for me
<rule name="IngnoreUTMSource" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="utm_source=.*" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{R:0}" appendQueryString="false" redirectType="Permanent" />
</rule>
How can we combine these two rules for IIS?
"mod_rewrite" - redirects all requests to the PHP script to index.php
The second is a classic redirect from http to https
But the trouble is that either one or the other works, the two rules do not work together, tell me how to combine them?
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<defaultDocument>
<files>
<clear />
<add value="index.php" />
</files>
</defaultDocument>
<rewrite>
<rules>
<rule name="Silex Front Controller" enabled="true" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" appendQueryString="true" />
</rule>
<rule name="http-to-https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
You could implement both of the rule at same time. you just need to change the order of the rule. place the https redirect rule at first:
<rule name="http-to-https" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />
</rule>
<rule name="Silex Front Controller" enabled="true" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" appendQueryString="true" />
</rule>
On IIS7, my website is not redirecting from www.example.com to https://example.com
Instead it tries to open https://www.example.com
Here is the web.config file
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="WWW to HTTPS">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="https://example.com/{R:1}" />
</rule>
<rule name="HTTP TO HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" />
</rule>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="backend-api/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{URL}" pattern="^/(backend-api)/.*" />
</conditions>
<action type="Rewrite" url="http://localhost:3033/{R:1}" />
</rule>
<rule name="AngularPageRefresh" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
<outboundRules>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>
What am I missing?
You can use the following rule:
<rule name="test1" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.(.+)$" />
</conditions>
<action type="Redirect" url="https://{C:1}" />
</rule>
I want ( https and non www) redirection for all requested URI. Any help would be highly appreciated. Thanks,
It works well for
1) 'http' to 'https' (success)
2) 'https' to 'https' (success)
3) '/index' to '/'
4) '/contact (with www)' to '/contact (with non www)' (unsuccess)
<system.web>
<customErrors mode="On" defaultRedirect="/" >
<error statusCode="404" redirect="/" />
</customErrors>
</system.web>
<system.webServer>
<rewrite>
<rewriteMaps>
<rewriteMap name="Redirects">
<add key="/index.php" value="/" />
<add key="/a.php" value="/a" />
<add key="/Res" value="/results#experience" />
<add key="/blog/" value="/" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}.php" matchType="IsFile" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="{R:1}.php" appendQueryString="true" />
</rule>
<rule name="Redirect rule1 for Redirects">
<match url=".*" />
<conditions>
<add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
<rule name="Remove www prefix">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://test.com.au/{R:1}" redirectType="Permanent"/>
</rule>
<rule name="Redirect index.php" stopProcessing="true">
<match url="^(\w*/)?index\.php" ignoreCase="true"></match>
<conditions>
<add input="{HTTP_HOST}" pattern="test\.com\.au$"/>
</conditions>
<action type="Redirect" url="https://test.com.au/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
<staticContent>
<clientCache cacheControlCustom="public" cacheControlMode="UseExpires" httpExpires="Tue,19 Jan 2038 03:14:07 GMT" />
<mimeMap fileExtension=".mp4" mimeType="video/mp4" />
<mimeMap fileExtension=".woff" mimeType="application/font-woff" />
<mimeMap fileExtension=".woff2" mimeType="application/font-woff2" />
</staticContent>
<defaultDocument>
<files>
<remove value="index.php" />
<add value="index.php" />
</files>
</defaultDocument>
</system.webServer>
I want all requested uri to redirect to https and non www via web.config only
Refer to the code as below:
<rewrite>
<rules>
<rule name="Redirect to non-www" stopProcessing="true">
<match url="(.*)" negate="false"></match>
<action type="Redirect" url="https://example.com/{R:1}"></action>
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" negate="true"></add>
</conditions>
</rule>
</rules>
</rewrite>
I can't figure out how to get my web.config deployment transformation to work for a rewrite rule. I've tried the following and it ignores it.
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.webServer>
<rewrite xdt:Transform="Replace">
<rules>
<rule name="Force HTTPS On Login/Register" stopProcessing="true">
<match url="Account/Login(.*)|Register(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:0}" redirectType="Permanent" />
</rule>
<rule name="Force HTTPS Off" stopProcessing="true">
<match url="((Account/Login(.*))|(Register(.*)))" negate="true" ignoreCase="true" />
<conditions>
<add input="{HTTPS}" pattern="^ON$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
I use SlowCheetah to transform my web.config for production. I originally tried what you tried, but found that I had to add an empty
<rewrite>
<rules />
</rewrite>
to the base web.config
and then write a transform like
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to HTTPS" stopProcessing="true" xdt:Transform="Insert">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
(that is a redirect transform, but I think the same principal should apply).
Note xdt:Transform="Insert" to insert a new node into the skeletal <rules /> in the base config file.