IIS Rewrite Rule - how to manipulate {HTTP_HOST} string - iis

I've just started using iis rewrite rules for the first time and I'm struggling with what I imagine is an easy rule.
Basically, I wish to use this rule
<rule name="redirect">
<match url="^(one$|two$|three$)" />
<action type="Redirect" url="{HTTP_HOST}{REQUEST_URI}" />
</rule>
But have the redirect {HTTP_HOST} string minus the extension [.co.uk / .com etc.].
e.g.
bigsite.co.uk/one
would redirect to:
bigsite.co.uk/bigsite/one
How does one go about this - for I can only find Tolower / UrlEncode / UrlDecode string manipulators?
[Ultimately, I would also like to then use a rewrite rule to hide the fact that the redirect has occurred, i.e. the address would remain as bigsite.co.uk/one after the redirect.]

Well I eventually came up with this (hope it may help someone):
<rule name="redirect">
<match url="^(one$|two$|three$)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?(.*).co.uk" />
</conditions>
<action type="Redirect" url="{C:2}{REQUEST_URI}" />
</rule>
...not sure if there is a better way, but it works perfectly for me - thanks all, PP

Related

IIS URL Rewrite for one spesific domain

I have a rewrite rule that redirects users from mydomain.com/reset/test123 to mydomain.com/module.aspx?key=test123.
The rule works fine, and look like this:
<rule name="Reset" stopProcessing="true">
<match url="^reset/([_0-9a-z-]+)" />
<action type="Redirect" url="/module.aspx?key={R:1}" appendQueryString="false" />
</rule>
The only problem is that my website has multiple domains, and the rule runs for all of them. I need the rule to only run for mydomain.com.
I tried to change the match url to ^mydomain.com/reset/([_0-9a-z-]+), but this don't work.
How can I make the redirect to only run for one spesific domain?
Just add a condition under your <rule> element:
<conditions>
<add input="{HTTP_HOST}" pattern="mydomain.com" />
</conditions>
See Documentation

How can i include a page in an IIS url rewrite rule (redirect)

I'm fairly new to url rewrite and trying to figure this thing out.
I have a website with the following structure:
http://localhost/virtualdirectory1/somepage.aspx?parameter1=x
http://localhost/virtualdirectory2/
Now I added a rule for a redirect (inbound rule):
matches pattern (regex) ^$ to make sure that if anybody goes to http://localhost that they are redirected to http://localhost/virtualdirectory1/somepage.aspx?parameter1=x
This also works fine for http://localhost/?paramter1=x which gets redirected properly.
This still allows me to approach http://localhost/virtualdirectory2/ directly.
However if the user goes to http://localhost/somepage.aspx?paramter1=x they are not redirected to the virtualdirectory1
Could give me some tips on how that can be done?
Thanks!
Is this rule achieve your requirement? It will redirect both http://localhost and http://localhost/somepage.aspx?paramter1=x but it won't redirect http://localhost/.
<rule name="redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^(/somepage\.aspx)?$" />
</conditions>
<action type="Redirect" url="virtualdirectory1/somepage.aspx?parameter1=x" appendQueryString="false" redirectType="Temporary" />
</rule>

Can anyone help me consolidate these IIS rewrite rules?

I've got some IIS rewrite rules in place, and everything is working fine. But i feel like i can consolidate them.
Here are my rules:
<rule name="Blog - Advice - root" patternSyntax="ExactMatch" stopProcessing="true">
<match url="advice" />
<action type="Rewrite" url="https://mysite.wpengine.com/advice" />
</rule>
<rule name="Blog - Advice - root (with slash)" patternSyntax="ExactMatch" stopProcessing="true">
<match url="advice/" />
<action type="Rewrite" url="https://mysite.wpengine.com/advice" />
</rule>
<rule name="Blog - Advice" stopProcessing="true">
<match url="^advice/?(.*)" />
<action type="Rewrite" url="https://mysite.wpengine.com/advice/{R:1}" />
</rule>
I want to match the following examples:
/advice
/advice/ (should rewrite without the slash)
/advice/sdfsdf
/advice/sdfsdf/ (should rewrite without the slash)
/advice/sdfdsf/sdfds
Can it be done with 1 (or 2) routes?
If i leave the second one out for example, Wordpress (the site i'm rewriting to), will do a 301 redirect to a URL without the slash, and i lose the rewrite since the URL is changed again. (so i end up at mysite.wpengine.com/advice, which is wrong).
So i need to make sure any rewrites don't involve a redirect on the target site end.
Many thanks :)

URL Alias in IIS 8.5 Using URL Rewrite Module

I need to create easy to remember URL's that redirect to long and hard to remember paths for a large number of applications.
i.e.
subdomain.domain.edu/shortname
redirects to
https://www.subdomain.domain.edu/mainApplication/subfolder/page
I'm using the URL Rewrite module in IIS 8.5, but I keep getting a 404 when I browse to the short alias. I know the rewrite module is working as I use it to handle rewriting HTTP to HTTPS and to add WWW to a URL.
My rewrite rule looks like:
<rewrite>
<rules>
<rule name="Easy to remember shortcut" stopProcessing="true">
<match url=".*subdomain.domain.edu/shortname" />
<conditions>
<add input="{URL}" pattern=".*/shortname" />
</conditions>
<action type="Redirect" url="https://www.subdomain.domain.edu/mainApplication/subfolder/page.aspx" />
</rule>
</rules>
</rewrite>
Of course this returns a 404. Any ideas?
Forgive me if there is already an answer to this in another post, however, I've read through and tried over 30 posts on the URL rewrite module and have not yet found the solution for actually creating an alias.
Url rewrite match condition won't be able to see your domain i.e. if the URL is https://www.subdomain.domain.edu/shortname the match part can only see shortname (anything after domainname/).
To validate the host we need to add in the conditions clause. So your rule will be something like below
<rule name="shortnameURL" enabled="true" stopProcessing="true">
<match url="shortname" />
<action type="Redirect" url="mainApplication/subfolder/page" />
<conditions>
<add input="{HTTP_HOST}" pattern=".*subdomain.domain.edu" />
</conditions>
</rule>
Also it should be ok if you add the entire URL here
<action type="Redirect" url="mainApplication/subfolder/page" /> as below
<action type="Redirect" url="https://www.subdomain.domain.edu/mainApplication/subfolder/page" />

url rewrite - remove subdomain only when directory meets criteria

I have seen a handful of similar questions on handling removing the www from a URL, but I was needing to remove it only when a certain directory exists. The real issue is that I run the "test" in URL Rewrite within IIS and it works fine, but for some reason does not respond when I type in the URL. I have tried switching to when "not matches" and it redirects, so I am a bit baffled what I am doing wrong to get this to work.
An example URL would be http://www3.test.com/feed/testing.xml
If the directory "feed" exists in the URL then remove the www3. I thought this would be fairly simple.
<rule name="fix feeds" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="(www3\.)(.*)(feed/.*)" />
</conditions>
<action type="Redirect" url="http://{C:2}{C:3}" appendQueryString="false" redirectType="Permanent" />
Another issue I am finding is that if you set it to permanently redirect 301, then once you get something to fire (and may not work as expected) the browser caches the 301 redirect and makes a mess of your testing environment.
You can't match feed in HTTP_HOST instead you need to use Match URL.
You have to match /feed/testing.xml in match URL which will yield here R:0
Match your patter if it has www3 and take C:2 which will give you URL without www3.
<rule name="fix feeds" enabled="true" stopProcessing="true">
<match url="^feed/.*" />
<conditions>
<add input="{HTTP_HOST}" pattern="(www3\.)(.*)" />
</conditions>
<action type="Redirect" url="http://{C:2}/{R:0}" appendQueryString="false" redirectType="Permanent" />
</rule>

Resources