IIS7.5 URL Rewrite Regex matching when it shouldn't - regex

I have split some pages in between subdomains and want to do a URL rewrite to different pages on different subdomains in certain cases. Everything is a rewrite rule except for the final two rules in the file. Those last two rules determine which subdomain to route the path I fixed to.
The way I am doing it is if I prepend the path with an underscore (_) then it stays on subdomain A. If I prepend the path with a tilde (~) then it is redirected to subdomain B.
So I have this rule:
<rule name="Login rule" stopProcessing="false">
<match url="(.*?)/?old-path/Login\.aspx$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_METHOD}" pattern="GET" />
</conditions>
<action type="Rewrite" url="~new-path/login.aspx" />
</rule>
Please notice there is an aspx on the end of the URL. It continues processing, but I have a generic rewrite rule at the end of the list right before the redirect ones. This is to remove all ASPX extensions on subdomain A (www), but I want to leave the ASPX extension for subdomain B (Please don't suggest removing the suggested on the 2nd subdomain. Thanks :)
<rule name="Remove ASPX" stopProcessing="false">
<match url="^([^www\.]+)\.aspx$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="_{R:1}" />
</rule>
The problem is, is this won't work because all the URLs have www in the beginning. I am not that good with regex, but I am guessing I need to just apply this rule to all URL that has a tilde in it. I tried this, but it's not really working either:
<match url="^_+\.aspx$" />
Basically I want this rule to ignore URLs that I have rewritten to have a ~ in them, but remove the ASPX if I placed the _ at the start of the path.
Any suggestions?

If I'm understood your problem then you have URL: "~new-path/login.aspx" and you want do redirect to "~new-path/login", right?
Then your regex should be like this:
^(.*~.*)\.aspx$
Note: "www" is a part of domain name and not included into matching.
So if your full URL is "http://www.mysite.com/~new-path/login.aspx" then only "~new-path/login.aspx" piece will take part in regex matching.
And template {R:1} will contain value in first group (braces): "~new-path/login"

Related

IIS Rewrite - redirect site to new domain capturing the language embedded in the URL

I am trying to write a regex to redirect the URL to a new domain. I wrote IIS Rewrite rule for this:
<rule name="Redirect to new domain" stopProcessing="true">
<match url="^(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)?my-www-en\.sites\.company\.net(\/([a-zA-Z]{2,3}-[a-zA-Z]{2,3}|en)\/?)?(.*$)" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://my-new-domain.com/en-us/{C:4}" appendQueryString="true" />
</rule>
It works fine when the language is not added to the initial URL, however, some of the pages have the language added after the domain which results in double language appearance in the end URL.
So basically I would like to redirect things like:
my-www-en.sites.company.net/some-page/another/page/
www.my-www-en.sites.company.net/some-page/another/page/
my-www-en.sites.company.net/de-de/some-page/another/page/
www.my-www-en.sites.company.net/de-de/some-page/another/page/
my-www-en.sites.company.net/en/some-page/another/page/
to redirect to:
https://my-new-domain.com/en-us/some-page/another/page/
My current regex does not capture these groups correctly (even when it does while testing the regex in IIS rewrite) and I struggle to make it work. Right now everything gets redirected to the homepage instead to particular websites. Could you please help?
Please try this rule. The regular expressions can match all urls above.
<rule name="test">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)?my-www-en\.sites\.company\.net$" />
<add input="{REQUEST_URI}" pattern="(/.*)?(/some-page/another/page/)" />
</conditions>
<action type="Rewrite" url="https://my-new-domain.com/en-us{C:2}" />
You can change rewrite to redirect.

IIS URL Rewrite: Add trailing slash in url only once

I have a rule in IIS to append a slash at the end (if there is no). It is working fine but in my case, I only need it for the first time. I have a reverse proxy on IIS to forward the request to another server. With this rule, it appends the slash all the time.
How I can modify the rule to append the slash only if there is not any after a keyword like 'myapp' so that it appends the slash if a URL is like http://myserver/myapp
<rule name="AddTrailingSlashRule1" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}/" />
</rule>
I tried changing the regular expression in url="(.*myapp[^/])$" but it does not work.
As far as I know the [^/] means there is a character which is not the '/'. So your regex will not work well for the myapp.
The right regex is .*myapp$.
Result:

IIS URL Rewrite Module Canonical Link Only If No Subdomain

I am using the IIS URL Rewrite module on a Windows 2012 server. I only want to add a canonical www rule if a subdomain is missing (or an exact match on 'example.com').
The problem is the ready-made Canonical rewrite rule IIS creates rewrites all traffic (*) for all subdomains to www.
<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" />
</rule>
The problem is that I have multiple subdomains / applications under one site (and therefore one web.config).
I want
example.com
to go to
www.example.com
but to leave these and any other subdomain alone:
secure.example.com
profile.example.com
Do I need a more specific match regex or pattern?
More specifically, the secure subdomain uses https and a better encryption certificate.
You're looking for everything that is not www.example.com. And that includes all other sub-domains.
Let's have a look at your condition:
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
If www.example.com comes in --> match = true --> negate --> false --> nothing is changed (as you want)
If example.com comes in --> match = false --> negate --> true --> URL gets redirected (as you want)
If foo.example.com comes in --> match = false --> negate --> true --> URL get redirected (as you don't want)
To fix this, try this configuration:
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{CACHE_URL}" pattern="^(.+)://" />
<add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" url="{C:1}://www.example.com/{R:1}" />
</rule>
The C:1 part is for the protocol (http and https).
Found here.
This regex is better:
^(w{3}\.)?\w+\.com/
This regex will give you the option to get url with and without the www. and the / at the end, but still wont math a subdomain.

Matching strings not ending with .html

I want to redirect my website users when they hit a REST path without the trailing slash.
Example.
http://mywebsite.my/it/products/brand/name => http://mywebsite.my/it/products/brand/name/
http://mywebsite.my/it/products => http://mywebsite.my/it/products/
http://mywebsite.my => http://mywebsite.my/
http://mywebsite.my/it/products/brand/name/code.html => ???
Well, I don't want the last one to be rewritten, I don't want the trailing slash when the URL ends with .html.
I'm working with URL rewrite module of IIS7, and this is my "slash-rule".
<rule name="SLASHFINALE" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule>
In other words, if the input url matches that regex (everything not ending with a slash), I rewrite the same URL adding the trailing slash.
So my rule would be the same, but with that little addition: rewrite all URLs, except the ones (already) having the trailing slash or the ones ending with ".html".
I wrote this
(.*(?<!html)[^\/])$
but I can't understand why it's not working.
IIS Javascript-flavored regex parser does not support conditional expressions.
I ended up with this:
<match url="(.*[^\.]...[^\/]$)" />
Enough for me.

Regex URL match on anything but www

I'm using IIS7 and the URL Rewrite module.
I would like to use regex to match any subdomain apart from www.
So...
frog.domain.co.uk = Match
m.domain.co.uk = Match
anything.domain.co.uk = Match
www.domain.co.uk = No match
This way I can redirect any subdomain that someone types in back to www.
you can use 301 in .htaccess for this.
This will match what you want:
^(?!=www\.).*
Which is a negative lookahead for www.. Not sure if you need the trailing .*
Use this rule -- it will redirect to www.exmaple.com domain if domain is different:
<system.webServer>
<rewrite>
<rules>
<rule name="Force www" stopProcessing="true">
<match url="(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.example\.com" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
You can optimize it a bit if you do not want to type domain name twice (example.com) -- but that is very minor thing and depending on your circumstances/configuration it is can be undesired.