IIS Redirect Regex start of string - regex

I am trying to redirect from bsl2 to bsl2 using IIS rewrite
http://server/site/bsl2/controllertest.aspx to
http://server/site/bsl3/controllertest.aspx
This rule works fine:
<rewrite>
<rules>
<rule name="BSL" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{PATH_INFO}" pattern="bsl2/(.*)$" />
</conditions>
<action type="Redirect" url="bls3/{C:1}?test={C:0}" />
</rule>
</rules>
</rewrite>
but it will also do server/site/otherbsl2/controllertest.aspx
I have tried using ^ to denote the start of the string but it does not work, e.g.
<add input="{PATH_INFO}" pattern="^(bsl2)/(.*)$" />
What am I doing wrong?

Use following regular expression:
/bsl2/(.*)$
or use word boundary \b:
\bbsl2/(.*)$

Related

Rewrite rule in web.config Umbraco

I like to use Umbraco with Vue.js and leave all the routing to Vue so I can make SPA. My goal is to write an IIS rewrite rule in web.config for using one Umbraco template for all routes except /umbraco. So far I have this
<rewrite>
<rules>
<rule name="Startview Templating">
<match url="^(http(s)?:\/\/)?(www\.)?([A-Za-z0-9:._-]*)(\/(?!umbraco)([A-Za-z0-9:_-]*))?" />
<action type="Rewrite" url="{R:0}?alttemplate=test" appendQueryString="true"/>
</rule>
</rules>
</rewrite>
This renders the same template (test)on every route I have so that works. But when I enter /umbraco I get a blank page. What am I missing?
Thanks
You should be able to add conditions to the rule to exclude paths, for example:
<conditions>
<add input="{REQUEST_URI}" pattern="^/umbraco/" negate="true" />
</conditions>
In your case i would look something like (I think you can simplify the match url though):
<rewrite>
<rules>
<rule name="Startview Templating">
<match url="^(http(s)?:\/\/)?(www\.)?([A-Za-z0-9:._-]*)(\/(?!umbraco)([A-Za-z0-9:_-]*))?" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/umbraco/" negate="true" />
</conditions>
<action type="Rewrite" url="{R:0}?alttemplate=test" appendQueryString="true"/>
</rule>
</rules>
</rewrite>

iis rewrite redirect all pages to homepage

I'm attempting to redirect all pages on a IIS site to the homepage/root. My rule below seems to work however I get a "localhost redirected you too many times" error.
The rule needs to exclude anything before the slash e.g mysite.com/mypage/
<rule name="redirect_all_bar_home_root" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{REQUEST_URI}" pattern="^$" negate="true"/>
</conditions>
<action type="Redirect" url="http://localhost" />
</rule>
This rule will do that:
<rule name="redirect_all_bar_home_root" stopProcessing="true">
<match url="^$" negate="true"/>
<action type="Redirect" url="/" />
</rule>
Regex: ^$ is matching when string is empty (it is homepage)
negate="true" is negating regexp. All URLs which are not homepage
I've just had a requirement to do this. The following rule will do what you need but with the exception that it will not attempt to redirect file urls so images still work on your homepage:
<rule name="RedirectAllToRoot" stopProcessing="true">
<match url="^$" negate="true" />
<action type="Redirect" url="/" redirectType="Temporary" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
</rule>
redirectType="Temporary" gives a 307 redirect status code

Rule not working in rewrite rules despite valid regex

Our webconfig file uses Url Rewrite, essentially pushing any http traffic to https
This works fine other than developing locally. For a while we have to simply remember to comment out the code from the web.config and uncomment it again for commit. Naturally this isn't a good way to work.
The code is simple
<rewrite>
<rules>
<rule name="Redirect-AllWWW-ToSecureNonWWW">
<match url="^((?!local).)*$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?:www\.)(.+)$" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:0}"/>
</rule>
<rule name="Redirect-AllNonSecure-ToSecureNonWWW-ExcludingLocalhost">
<match url="^((?!local).)*$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^localhost$" negate="true" />
<add input="{HTTPS}" pattern="^off$" />
<add input="{HTTP_HOST}" pattern="^(?:www\.)?(.+)" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:0}" />
</rule>
</rules>
</rewrite>
And as per the regex101 , it works!
https://regex101.com/r/3Mz6w1/1
However, when on localhost, I am still directed to HTTPS
Why does it work in regex101 and not in my web.config file
This seems to be related to Redirect rule not working
quote from URL Rewrite Module Configuration Reference
A rewrite rule pattern is used to specify a pattern to which the current URL path is compared.
...
A pattern is specified within a <match> element of a rewrite rule.
According to this piece of official information, you must be sure that <match url compares only with URL paths which never contain host names, not the entire URL.
For Url Rewrite Module, URL path of this question is questions/44944175/rule-not-working-in-rewrite-rules-despite-valid-regex for example. No stackoverflow.com no https:// no query strings but only the path without leading slash.
To ignore requests for host names containing local you need some conditions looking for a local match with HTTP_HOST header.
<rewrite>
<rules>
<rule name="Redirect-AllWWW-ToSecureNonWWW" stopProcessing="true">
<match url=".*" />
<conditions>
<!-- continue if http host name does not contain "local" -->
<add input="{HTTP_HOST}" pattern="local" negate="true" />
<add input="{HTTP_HOST}" pattern="^(?:www\.)(.+)$" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:0}" />
</rule>
<rule name="Redirect-AllNonSecure-ToSecureNonWWW-ExcludingLocalhost" stopProcessing="true">
<match url=".*" />
<conditions>
<!-- continue if http host name does not contain "local" -->
<add input="{HTTP_HOST}" pattern="local" negate="true" />
<add input="{HTTPS}" pattern="^off$" />
<add input="{HTTP_HOST}" pattern="^(?:www\.)?(.+)" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:0}" />
</rule>
</rules>
</rewrite>

IIS - Configuration file is not well-formed XML

<rule name="Posts2" enabled="true">
<match url="^index.cfm\?section=latest.news&id=(.*)" />
<action type="Rewrite" url="post.php?id={R:1}" />
</rule>
Problem is with the regex - any idea at all? I can't seem to work it out. I've run it on http://www.xmlvalidation.com/ and it says
The reference to entity "id" must end with the ';' delimiter.
When I change the ampersand to & the rewrite doesn't seem to work on URLs such as /index.cfm?section=latest.news&id=14726 despite it passing the test in IIS. Even when I escape the dots and question mark it doesn't work
Try using a {QUERY_STRING} input using conditions perhaps:
<rule name="Posts2">
<match url="index\.cfm$" />
<conditions>
<add input="{QUERY_STRING}" pattern="section=(latest\.news)" />
<add input="#{C:1}#_{QUERY_STRING}" pattern="#([^#]+)#_.*id=(\d+)" />
</conditions>
<action type="rewrite" url="post.php?id={C:2}" appendQueryString="false"/>
</rule>
URL's containing query strings are usually better handled this way, although if you really prefer not going this route then make sure you have the all the proper tags included, and also try escaping the . in between latest\.news.
<rewrite>
<rules>
<rule name="Posts2">
<match url="^index\.cfm\?section=latest\.news&id=(\d+)" />
<action type="Rewrite" url="post.php?id={R:1}" />
</rule>
</rules>
</rewrite>

Rewrite rule to HTTPS except when on localhost

I am using the answer given here as the basis for trying to add a rewrite rule to my web.config file. I want it to match any url that is not running on localhost in order to force https.
Here is what I have right now:
<system.webServer>
<rewrite> <!-- force https - https://stackoverflow.com/a/15119044/51 -->
<rules>
<rule name="Redirect HTTP to HTTPS" stopProcessing="true">
<match url="^((?!localhost).)*$"/>
<conditions>
<add input="{HTTPS}" pattern="^OFF$"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
</rule>
</rules>
</rewrite>
</system.webServer>
I am trying to use a negative lookaround in order to only match url's that do not include "localhost" within the url. But this is not working.
So how should this rule be set up in order to only rewrite non-localhost url's?
Try this condition:
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect HTTP to HTTPS" stopProcessing="true">
<match url="^(.*)$"/>
<conditions>
<add input="{HTTPS}" pattern="^OFF$"/>
<add input="{HTTP_HOST}" matchType="Pattern" pattern="^localhost$" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
</rule>
</rules>
</rewrite>
</system.webServer>
Using a negate condition against the localhost pattern should do the trick.
Adding to anubhava's answer, you can replace the add element for localhost with the following 2 entries to cater for both localhost and 127.0.0.1 with optional ports e.g localhost:59400 which is the case when debugging through visual studio and IIS
<add input="{HTTP_HOST}" matchType="Pattern" pattern="^localhost(:\d+)?$" negate="true" />
<add input="{HTTP_HOST}" matchType="Pattern" pattern="^127\.0\.0\.1(:\d+)?$" negate="true" />
With the original answer, localhost:123 would be redirected to https which may not be desirable.
Regular expression updated, this works for me, with a custom Port
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{HTTP_HOST}" matchType="Pattern" pattern="(localhost)(:\d+)" negate="true" />
<add input="{HTTP_HOST}" matchType="Pattern" pattern="(127.0.0.1)(:\d+)" negate="true" />