Can anyone help me consolidate these IIS rewrite rules? - iis

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 :)

Related

IIS and URL Rewrite for landing pages and simple URLs

Right now we have a bunch of extra directories with an index.htm in them that handle redirects to pages within our application. Instead of littering our folder structure with these, is there a way to us URL rewrite to redirect a user without completely breaking 404 errors?
For example,
domain.com/login
Would redirect to
domain.com/user/accountLogin.htm
Or
domain.com/happypartnercompany
Could go to
domain.com/partners/about.xyz?partner=123
For anyone else looking for a simple example, this is what I ended up doing:
<rule name="members" stopProcessing="true">
<match url="^members*$" ignoreCase="true" />
<action type="Rewrite" url="index.xyz?m=home/members/list" />
</rule>
And to actually redirect the user:
<rule name="member_signup_1" stopProcessing="true">
<match url="^sefcu*$" ignoreCase="true" />
<action type="Redirect" url="index.xyz?m=signup/enroll&partnerid=8532" />
</rule>
Ampersands must be escaped with & in the URL.

IIS Domain rewrite to subfolder (only home page)

I want my domain homepage to be redirected to one of my mvc directories
this is the rule i am using
<rule name="Home" stopProcessing="true">
<match url="^/*$" />
<action type="Rewrite" url="/test/" logRewrittenUrl="true" />
</rule>
Same rule is working if i put redirect. But rewrite is not working.
Other URLS should be served normally. Only home page should be rewritten
Can we achieve this without ARR?
//www.example.com should be rewritten to //www.example.com/test/
www.example.com/buy should be served as it is.
Your regex is incorrect. Correct rule is:
<rule name="Home" stopProcessing="true">
<match url="^$" />
<action type="Rewrite" url="/test/" logRewrittenUrl="true" />
</rule>
And this rule will work without ARR because you rewriting to a subfolder in the same website. If you want to rewrite to the different website in IIS, then you need ARR.

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" />

IIS Rewrite Rule - how to manipulate {HTTP_HOST} string

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

Simple IIS url rewrite not working

I am trying to hide a subdirectory where I installed a framework. While this works like a charm in .htaccess, I am getting stuck with IIS rules. Basically I want to go from
http://www.mysite.com/CI/client-tracking to
http://www.mysite.com/client-tracking
I am using this rule
<rule name="Redirect to Codeigniter" stopProcessing="true">
<match url="^client-tracking$" ignoreCase="false" />
<action type="Rewrite" url="/CI/client-tracking" />
</rule>
LE: I am not in need of a redirect, I need to rewrite the URL and eliminate the \CI directory from it, so the user and search engines see a nice link.
Did you try removing the leading "/" on the re-written url
<rule name="Redirect to Codeigniter" stopProcessing="true">
<match url="^client-tracking$" ignoreCase="false" />
<action type="Rewrite" url="CI/client-tracking" />
</rule>

Resources