Rewrite rule in web.config Umbraco - regex

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>

Related

Building IIS Redirect Rule to Redirect Product Page to New URL Passing Product ID

I'm attempting to use the URL Rules engine in IIS 7 to redirect our current product pages to our new product pages.
Old Pages:
example.com/itemform.aspx?item=X12878
example.com/itemform.aspx?item=Y87304&showmenu=T
New URL
example.com/c/product/X12878
example.com/c/product/Y87304
Attempt
<rule name="Product Page Redirect">
<match url="/itemform\.aspx\?item=([.a-zA-Z0-9]+)$" />
<action type="Redirect" url="https://newsite.com/x/product/{C:1}" redirectType="Permanent" />
</rule>
Complete Rewrite Rule w/ Suggestion by Emma
<rewrite>
<rules>
<rule name="Product Page Redirect" stopProcessing="true">
<match url="/itemform\.aspx\?item=([A-Za-z0-9.]+)" />
<conditions>
</conditions>
<action type="Redirect" url="https://newsite.com/x/product/{R:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Accoridng to your description, if you want to use url rewrite to get the right query string.
More details, you could refer to below codes:
<rule name="QueryStringRue" enabled="true" stopProcessing="true">
<match url="itemform.aspx" />
<conditions>
<add input="{QUERY_STRING}" pattern="^item=([0-9a-zA-Z]*)" />
</conditions>
<action type="Redirect" url="http://example.com/c/product/{C:1}" appendQueryString="false" />
</rule>
Result:
My guess is that maybe removing the end $ anchor might simply make the match valid:
<rule name="Product Page Redirect">
<match url="/itemform\.aspx\?item=([A-Za-z0-9.]+)" />
<action type="Redirect" url="https://newsite.com/x/product/{C:1}" redirectType="Permanent" />
</rule>
but, maybe not.

IIS rewrite rule not working in live environment

I have 4 servers in azure, 3 are load balanced and the 4th is for CMS purposes only.
SSL certificate has been added for the main website, but not for the sobdomain that the CMS is on.
I wrote a rule that should find any url that doesnt contain "backoffice" and match any other page to change it to https.
This works on regexr.com but for some reason doesnt work
<rewrite>
<rules>
<rule name="http to https" stopProcessing="true">
<match url="(https?:\/\/(?!backoffice).*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://www.WEBSITENAME.com{R:1}" />
</rule>
</rules>
</rewrite>
Url Rewriting 2.1 is installed on all 4 servers and i have created a load balance set in azure for https.
going to https manually works fine (along with loadbalancing).
Additional information:
I've tried many rules, including the existing answer. I can see things happening, like assets being brought in as https, but the page itself does not redirect.
There are 2 load balance sets, one for port 80 and the other for port 443. I don't know if this is corect, or could be a potential cause in the redirect not happening.
Your rule should be like that:
<rule name="http to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{REQUEST_URI}" pattern="/backoffice" negate="true" />
</conditions>
<action type="Redirect" url="https://www.WEBSITENAME.com{R:0}" />
</rule>
This rule will exclude requests with /backoffice path.
Also for issue of mixing content you need to fix your paths for css/js/images to relatives. Example:
<img src="/path/to/your/image.jpg"/>
Another way to fix mixed content is create outbound rule, which will change your output HTML (replace http: to https:):
<rewrite>
...
<outboundRules>
<rule name="Rewrite external references to use HTTPS" preCondition="IsHTML">
<match filterByTags="Script, Link, Img, CustomTags" customTags="HTML5Tags" pattern="^http://(.*)$" />
<action type="Rewrite" value="https://{R:1}" />
</rule>
<preConditions>
<preCondition name="IsHTML">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
<customTags>
<tags name="HTML5Tags">
<tag name="Video" attribute="src" />
</tags>
</customTags>
</outboundRules>
</rewrite>
Using the previous answer as a starting point, i made a few minor changes, to use HTTP_HOST rather than REQUEST_URI for the pattern negation and it works.
<system.webServer>
<rewrite xdt:Transform="InsertIfMissing">
<rules>
<rule name="http to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="^backoffice\.WEBSITENAME\.com$" negate="true" />
</conditions>
<action type="Redirect" url="https://www.WEBSITENAME.com/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>

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>

Web.config rewrite module

I am trying to figure out how to add a rule to my web.config so it detects any link that has anything in the url beyond the .com portion - and then does not process the next input condition which detects if the device is mobile and then redirects to m.mysite.com.
For example here are some example cases I would want it to skip over the mobile redirect:
If a incoming link is www.mysite.com/coupon/etc
If a incoming link is www.mysite.com?c=1
I do want it to redirect to the mobile site in all cases where its just the base domain of www.mysite.com or mysite.com.
Here is my current web.config rewrite section.
<rewrite>
<rules>
<rule name="MobileRedirect" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" ignoreCase="true"/>
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_COOKIE}" pattern="persistdesktop=1" ignoreCase="true" negate="true"/>
<add input="{HTTP_USER_AGENT}" pattern="android|blackberry|googlebot-mobile|iemobile|iphone|ipod|opera mobile|palmos|webos"/>
</conditions>
<action type="Redirect" url="http://m.mysite.com" appendQueryString="false" redirectType="Found"/>
</rule>
</rules>
So my best guess is that I need to add another condition above the other two. And then do I change the conditions logicalGrouping parameter to MatchAny? Or do I create another rule above this existing rule?
Thanks!
You haven't set the ignoreCase attribute value to true for HTTP_USER_AGENT. The match URL pattern and the Query string pattern need some changes.
Change the rewrite configuration to:
<rewrite>
<rules>
<rule name="MobileRedirect" stopProcessing="true">
<match url="^$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_COOKIE}" pattern="persistdesktop=1" negate="true" />
<add input="{HTTP_USER_AGENT}" pattern="android|blackberry|googlebot-mobile|iemobile|iphone|ipod|opera" ignoreCase="true"/>
<add input="{QUERY_STRING}" pattern="^$"/>
</conditions>
<action type="Redirect" url="http://m.mysite.com" redirectType="Found" />
</rule>
</rules>
</rewrite>

How redirect all web-site to other domain?

I have a test domain and it indexed in goolge, but it is wrong.
Now I need to create 301 redirect from all pages with all parameters to other domain.
I could to create action filter and create redirect on it, but it is wrong for me too.
I have known what it can implement in webconfig. But I don't understand how.
Do you help me please.
I try to use:
<rewrite>
<rules>
<rule name="Redirect to main subdomain from test domain">
<match url="^.*$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)uh261477\.ukrdomen\.com$" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="http://kitesurfing-base.com{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
But it not work
You can place this rule in your web.config for this redirect:
<rule name="Redirect to main subdomain from test domain">
<match url="^.*$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)testdomain\.com$" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="http://otherdomain.com{R:0}" redirectType="Permanent" />
</rule>