URL Rewrite-rule in IIS 8 not triggering - iis

I have a problem with my URL rewrite and I don't know what I'm doing wrong so, maybe you can point me in the right direction. We had a intranet-site, which had a pattern like this: intranet.old-site.com. Now we got a new domain and I want to forward my outdated links to the front-page of our new intranet, which looks like: intranet.new-site.com.
I installed the URL Rewrite module in IIS and in my point of view, the setup is correct:
Match URL
Request URL: matches pattern.
Using: Wildcards
Pattern: *intranet.old-site*
Ignore case: true
Conditions
none
Server Variables
none
Action
Action Type: Rewrite
Action Properties: Rewrite-URL: http://intranet.new-site.com/
Append query string: true
Log rewritten URL: false
Stop processing of subsequent rules: false
I'm also open for any idea what might work, so if there is a rule for the web.config, I could try that as well.
Update 1:
the web-config looks like this now:
<rewrite>
<rules>
<rule name="RewriteSG2DE" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*).sgbdd.saint-gobain(.*)" />
<action type="Redirect" url="intranet.stark-deutschland.net" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="(.*)sgbdd.saint-gobain(.*)" />
<add input="{HTTP_HOST}" pattern="intranet.sgbdd.saint-gobain.com" />
</conditions>
</rule>
</rules>
</rewrite>
I want any site, which is basically anything like intranet.sgbdd.saint-gobain.com/start.asp?something_more" to be forwarded / redirected to intranet.stark-deutschland.net/start.asp?something_more
For instance:
intranet.sgbdd.saint-gobain.com/start.asp?something_more --> intranet.stark-deutschland.net/start.asp?something_more
somesite.intranet.saint-gobain.com/my/new/site --> somesite.intranet.stark-deutschland.net/my/new/site
I also added conditions as suggested:
Thanks for you input

Using two URL rewrite rules is much easier to handle this.
If you want to redirect
intranet.sgbdd.saint-gobain.com/start.asp?something_more --> intranet.stark-deutschland.net/start.asp?something_more
somesite.intranet.saint-gobain.com/my/new/site --> somesite.intranet.stark-deutschland.net/my/new/site
Then rule can be like this.
<rule name="rule1" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="^(.*)saint-gobain.com$" />
<add input="{HTTP_HOST}" pattern="intranet.sgbdd.saint-gobain.com" negate="true" />
</conditions>
<action type="Redirect" url="http://{C:1}stark-deutschland.net/{R:1}" redirectType="Temporary" />
</rule>
<rule name="rule2" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^intranet.sgbdd.saint-gobain.com$" />
</conditions>
<action type="Redirect" url="http://intranet.stark-deutschland.net/{R:1}" redirectType="Temporary" />
</rule>
I tried to combine two rules to one rule but there's something wrong with expression ?! so it is recommended to split into two rules.
If you means
intranet.sgbdd.saint-gobain.com/start.asp?something_more --> intranet.stark-deutschland.net/start.asp?something_more
somesite.intranet.sgbdd.saint-gobain.com/my/new/site --> somesite.intranet.stark-deutschland.net/my/new/site
Then the rule can be like this.
<rule name="RewriteSG2DE" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="http://{C:1}stark-deutschland.net/{R:1}" redirectType="Temporary" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="(.*)sgbdd.saint-gobain.com" />
</conditions>
</rule>

Related

IIS Redirect to 404 all paths except one

I have multiple domains bound to one site. For one domain, I would like to leave everything untouched by the Rewrite module. For the other domain, for example: www.example.com, I wish to allow every request down this path www.example.com/allowedpath yet for any other requests from www.example.com and below, return a 404.
From your description, I understand that you want to achieve requirement below.
Domain1.com -> Should not be affected by Rewrite rule
Example.com -> Example.com/allowed_path
Example.com/any_other_path -> Abort request
To achieve the above requirement, I have created 2 rewrite rules below.
<rules>
<clear />
<rule name="Rule-1" stopProcessing="true">
<match url="^$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="example.com" />
</conditions>
<action type="Redirect" url="/test_path" />
</rule>
<rule name="Rule-2" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="example.com" />
<add input="{REQUEST_URI}" pattern="/test_path" negate="true" />
</conditions>
<action type="AbortRequest" />
</rule>
</rules>
Test result with Domain1.com
Test result with Example.com
Further, you could modify these rules as per your own requirement.

IIS 7 Redirect to new domain

Good morning,
I am trying to create a Rewrite rule in IIS to redirect a specific page to a new url however when I apply the rule no redirect happens what am I missing? Code:
</rule>
<rule name="Redirect" enabled="false" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^(/)?$" />
<add input="{HTTP_HOST}" pattern="domain1/server/sdk/" />
</conditions>
<action type="Redirect" url="https://domain2/rest/" />
</rule>
Try adding REQUEST_URI to the pattern match
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="domain1/server/sdk/" />
match url may also work better as
<match url="^(.*)$" />
also make sure the rule is enabled, your version has enabled="false" on it
Here is my full rule
<rule name="domain1/server/sdk/" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="domain1/server/sdk/" />
</conditions>
<action type="Redirect" url="https://domain2/rest/" redirectType="Permanent" appendQueryString="false" />
</rule>
Excellent thank you very much Mike, I did disable my rule after testing it and it did not work which is why it shows enabled=false however I revised the rule per your advice and that worked for me! Incase this is helpful for anyone else in the future my full rule is:
<rules>
<remove name="Portal Redirect" />
<rule name="(rule name)" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<action type="Redirect" url="Domain2/rest/" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="domain1/server/sdk/rest/" />
</conditions>
</rule>

URL Rewriting the path in IIS

I have a site that uses two characters in the URL path to determine the initial language e.g. https://my.company.net/Monitoring/gb displays in the English language. I want to setup a redirect for these two display the full culture code e.g. https://my.company.net/Monitoring/en-GB
This is the rule that I've tried:
<rewrite>
<rules>
<rule name="Monitoring gb rewrite" patternSyntax="ExactMatch" stopProcessing="true">
<match url="https://my.company.net/Monitoring/gb" />
<conditions />
<serverVariables />
<action type="Redirect" url="https://my.company.net/Monitoring/en-GB" appendQueryString="false" />
</rule>
</rules>
</rewrite>
I expected https://my.company.net/Monitoring/gb to redirect to https://my.company.net/Monitoring/en-GB however this rule does not have any effect: the browser URL stays at https://my.company.net/Monitoring/gb.
How can I rectify this?
You could use below url rewrite rule.
<rule name="gb to en-gb redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="ww.sample1.com" />
<add input="{HTTPS}" pattern="on" />
<add input="{REQUEST_URI}" pattern="Monitoring/gb" />
</conditions>
<action type="Redirect" url="https://www.sample1.com/Monitoring/en-GB" />
</rule>
Note: use your hostname instead of the www.sample1.com.
The redirect failure was due to browser caching. The redirect works once the cache is cleared.

Url Rewrite rules and the Piranha Manager interface

Working on my site today I added in some of the usual rewrite rules I use for converting urls to all lower case and also adding a trailing slash to the url should it not have one.
On doing this I could no longer access the Manager interface. The css disappeared for the login page and when logging in to didn't work and redirected me to the home page.
I have added some rewrite rules to work around this but was wondering if there was a better way to be doing it that you have already done?
The rewrite are shown below if you think this is a viable solution and want to use them in a Gist.
Note that the first 2 rules are for stopping processing when accessing the manager interface and the last two are just a couple of out of the box ones from IIS. One final point. I normally have trailing slashes but when working with Piranha had to enforce no trailing spaces to get to the manager interface once logged in.
<rewrite>
<rules>
<clear />
<rule name="IgnorePiranhaAreas" patternSyntax="ECMAScript" stopProcessing="true">
<match url="areas/manager" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="None" />
</rule>
<rule name="IgnorePiranhaManager" stopProcessing="true">
<match url="/manager" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="None" />
</rule>
<rule name="LowerCaseRule1" stopProcessing="true">
<match url="[A-Z]" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="{ToLower:{URL}}" />
</rule>
<rule name="RemoveTrailingSlashRule1" stopProcessing="true">
<match url="(.*)/$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}" />
</rule>
</rules>
</rewrite>
Your RemoveTrailingSkalRulel1 is screwing it up. Just remove it and you're fine.

IIS rewrite rules skipped even though the pattern matches

Sorry for the vague title, the problem is too complex to summarize in a short phrase...
I'm trying to set up the following redirection rules:
blog.mydomain.net/en/something: redirected to www.mydomain.com/something
blog.mydomain.net/fr/something: redirected to www.mydomain.fr/something
blog.mydomain.net/*: redirected to www.mydomain.com
Rule 3 is working, but rules 1 and 2 seem to be skipped so rule 3 is always applied. Here are my web.config rules:
<!-- Canonicalize mydomain.com to www.mydomain.com -->
<rule name="CanonicalHostNameRule_en" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^mydomain\.com$" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/{R:1}" />
</rule>
<!-- Canonicalize mydomain.fr to www.mydomain.fr -->
<rule name="CanonicalHostNameRule_fr" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^mydomain\.fr$" />
</conditions>
<action type="Redirect" url="http://www.mydomain.fr/{R:1}" />
</rule>
<!-- Redirect blog.mydomain.net/en/something to www.mydomain.com/something -->
<rule name="RedirectBlog_en" enabled="true" stopProcessing="true">
<match url="^/en(/.*)?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^blog\.mydomain\.net$" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/{R:1}" />
</rule>
<!-- Redirect blog.mydomain.net/fr/something to www.mydomain.fr/something -->
<rule name="RedirectBlog_fr" enabled="true" stopProcessing="true">
<match url="^/fr(/.*)?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^blog\.mydomain\.net$" />
</conditions>
<action type="Redirect" url="http://www.mydomain.fr/{R:1}" />
</rule>
<!-- Redirect blog.mydomain.net/* to www.mydomain.com -->
<rule name="RedirectBlog_other" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^blog\.mydomain\.net$" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/" />
</rule>
<!-- Wordpress-specific rules -->
...
I don't understand why rules RedirectBlog_en and RedirectBlog_fr are skipped; I tested the regular expressions and they work fine.
Can anyone spot the problem?
EDIT: if I disable the 3rd rule (RedirectBlog_other), then rules 1 and 2 work fine... how is it possible, since rules 1 and 2 are executed before rule 3?
OK, I got it!
First, things weren't happening as I thought; rules 1 and 2 were not working when I disabled rule 3: I was still redirected to my actual domain, but this was done by Wordpress, not by my rules.
Second, my pattern for matching the URL was wrong: the leading '/' is not included in the input, so my rules weren't matching at all.

Resources