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.
Related
I need to extract all the vanity URLs redirect rules but exclude the redirect rules.
<rule name="welcome2020" stopProcessing="true">
<match url="welcome2020" />
<action type="Redirect" url="https://www.mywebsite.org/Pages/.welcome2020aspx" appendQueryString="false" />
</rule>
<rule name="Page to Page Redirect">
<match url="/Staff/Pages/Ashley.aspx" />
<action type="Rewrite" url="services/Staff/Pages/Ashley.aspx" />
</rule>
i need to match all <match url="whatever" /> types that don't contain .aspx The only thing I have figured out is that I will need a negative lookahead. but not sure how to implement it.
I basically need something like this, but for my redirect rules.
https://www.regextester.com/15
You can just try:
^(?=.*match)(?!.*\.aspx).*
Please see the DEMO :)
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.
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.
I need a regex that will match all https URLs except for a certain path.
e.g.
Match
https://www.domain.com/blog
https://www.domain.com
Do Not Match
https://www.domain.com/forms/*
This is what I have so far:
<rule name="Redirect from HTTPS to HTTP excluding /forms" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{URL}" pattern="^https://[^/]+(/(?!(forms/|forms$)).*)?$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{R:0}" redirectType="Permanent" />
</rule>
But it doesn't work
The way the redirect module works, you should simply use:
<rule name="Redirect from HTTPS to HTTP excluding /forms" stopProcessing="true">
<match url="^forms/?" negate="true" />
<conditions>
<add input="{HTTPS}" pattern="^ON$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{R:0}" />
</rule>
The rule will trigger the redirect to HTTP only if the request was HTTPS and if the path wasn't starting with forms/ or forms (using the negate="true" option).
You could also add a condition for the host to match www.example.com as following:
<rule name="Redirect from HTTPS to HTTP excluding /forms" stopProcessing="true">
<match url="^forms/?" negate="true" />
<conditions>
<add input="{HTTPS}" pattern="^ON$" />
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{R:0}" />
</rule>
Does this give you the behavior you're looking for?
https?://[^/]+($|/(?!forms)/?.*$)
After the www.domain.com bit, it's looking for either the end of the string, or for a slash and then something that ISN'T forms.
I came up with the following pattern: ^https://[^/]+(/(?!form/|form$).*)?$
Explanation:
^ : match begin of string
https:// : match https://
[^/]+ : match anything except forward slash one or more times
( : start matching group 1
/ : match /
(?! : negative lookahead
form/ : check if there is no form/
| : or
form$ : check if there is no form at the end of the string
) : end negative lookahead
.* : match everything zero or more times
) : end matching group 1
? : make the previous token optional
$ : match end of line
I see two issues in the posted pattern http://[^/]+($|/(?!forms)/?.*$)
It misses redirecting URLs such as https://domain.com/forms_instructions, since the pattern fails to match those also.
I believe you have http and https reversed between the pattern and the URL. The pattern should have https and the URL http.
Perhaps this will work as you intend:
<rule name="Redirect from HTTPS to HTTP excluding /forms" enabled="true" stopProcessing="true">
<match url="^https://[^/]+(/(?!(forms/|forms$)).*)?$" />
<action type="Redirect" url="http://{HTTP_HOST}{R:1}" redirectType="Permanent" />
</rule>
Edit: I've moved the pattern to the tag itself since matching everything with .* and then using an additional condition seems unnecessary. I've also changed the redirection URL to use the part of the input URL captured by the brackets in the match.
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"