How to rewrite part of the URL and then attach the remaining? - url

For example, rewrite from www.example.com/foo/123 to www.example.com/bar/123
In web.config I have something like:
<rewriteMap name="TestRM">
<add key="/foo/" value="/bar" />
</rewriteMap>
<rule name="TEST" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{TestRM:{PATH_INFO}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="http://www.example.com{C:1}" appendQueryString="false" redirectType="Temporary" />
</rule>
With this is place, www.example.com/foo can be redirected to www.example.com/bar, but no www.example.com/foo/123 to www.example.com/bar/123
Anything I need to add to make the last part attached?

One of the strategy you can use is to use regex and separate the URL Parts.
For example URL: www.example.com/foo/123
Regex: ^(/foo)(/.*)? will separate out /foo and /123 or /123/3434/ or anything after /foo
Then you can craft the redirect URL as Such:
{HTTP-HOST}/bar{R:2}

Related

IIS Rewrite and reverse proxy for multiple applications

I have an IIS 10 with a website configured on that. There are multiple Applications underneath that website.
MyWebsite/app1 - MyWebsite/app2 - MyWebsite/app3
I Have another server(let's call it EndServer) hosting 3 websites on 3 different ports.
well, now what I wanna do is using IIS as a reverse proxy to redirect and MASK the application 1 to one of those websites in 2nd server and application 2 to another one.
at the end, Users will enter https://mywebsite/app1 and they will see the contents of website 1 in the Endserver.
Note: it is important for me that end Users see the URL like as https://mywebsite/app1/
how shall I edit the Rule below:
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://endserver:8052/{R:1}" />
<conditions trackAllCaptures="true">
</conditions>
</rule>
</rewrite>
Thanks
A.
According to your description, I suggest you could try to use below url rewrite rule.
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{URL}" pattern="^/app1/(.*)" />
</conditions>
<action type="Rewrite" url="http://endserver:8052/{C:1}" />
</rule>
<rule name="ReverseProxyInboundRule2" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{URL}" pattern="^/app2/(.*)" />
</conditions>
<action type="Rewrite" url="http://endserver:8053/{C:1}" />
</rule>
Thanks for your answer.
I got same error. as you see below /App1 is missing in the URL after localhost:Port
localhost:8888/assets/styles/Custom.css?m=1549903616.0
I believe the trouble is with rewriting the response URL. I don't know who can I add the missing part in URL.
Regards.

IIS URL redirection - igonre match all for some URLs

I had to change previous URL pattern to something else. Earlier URL pattern was
www.testdomain.com/hotels/"city" and it changed to www.testdomain.com/hotel-"city"
Below is the redirection I have used that for.
<rule name="cityChange" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^hotels/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="www.testdomain.com" />
</conditions>
<action type="Redirect" url="www.testdomain.com/hotel-{R:1}"appendQueryString="false" redirectType="Temporary" />
</rule>
Now I have another issue which is another URL I have used for the site is also redirecting to different one. That URL is,
www.testdomain.com/hotels/'city'/'name'-'street'-'postalcode'
Since I have used a wildcard for /hotels/'city' to /hotel-'city' it changes above URL as
www.testdomain.com/hotel-'city'/'name'-'street'-'postalcode'
as well. But that URL is not valid in the site and I do not want to replace "/" from "-" there.
How can I exclude this URL pattern from that wild card which I have mentioned above?
Thanks in advance.
Can't be sure about that but did you try to add parentheses, like that ?
<match url="^(hotels)/(.*)" />
We did that for ou specific wildcard redirect :
<rule name="modeles-en" enabled="true" stopProcessing="true">
<match url="^(modeles-en)/(.*)" ignoreCase="true" />
<action type="Redirect" url="/models/{R:2}" redirectType="Permanent" />
</rule>
Hope it helps !

IIS URL Rewriting rule - exclude ALL files and a specific path

I want to create a URL rewrite rule that adds a / on URLs that don't have one so for example:
www.domain.com/news/latest will get redirected to www.domain.com/news/latest/
The rule below does exactly that, but the problem I am having is two fold:
This rewrite rule is getting applied to things like image files.
So For example domain.com/globalassets/icons/image.svg gets changed to domain.com/globalassets/icons/image.svg/ causing a 404 its not happening with CSS files which is strange, maybe because I am adding them using the RegisterBundles method in MVC?
This is a ASP.NET MVC based website using a CMS (episerver) so I want to ignore any redirects in the Admin area so I added a second rule, but again its doing this to the CSS and images breaking the admin area.
This is what I have got so far, can anyone help me get this rule working correctly?
<rewrite>
<rules>
<rule name="Exclude Slash Episerver " stopProcessing="true">
<match url="^episerver/" />
<action type="None" />
</rule>
<rule name="Add trailing slash" 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>
</rules>
</rewrite>
Common SEO rewrite rules for IIS such as this one are documented here.
In particular, with the Trailing Slash rule you are missing the logicalGrouping="MatchAll" attribute:
Conditions are defined within a <conditions> collection of a rewrite rule. This collection has an attribute called logicalGrouping that controls how conditions are evaluated. If a rule has conditions, then the rule action is performed only if rule pattern is matched and:
All conditions were evaluated as true, provided that logicalGrouping="MatchAll" was used.
At least one of the conditions was evaluated as true, provided that logicalGrouping="MatchAny" was used.
Without this setting, it is adding a trailing slash to your file names because it is matching when any of your negated rules match rather than all of them.
The entire Trailing Slash rule from the above link is:
<rule name="Trailing Slash" 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" />
<add input="{URL}" pattern="WebResource.axd" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}/" />
</rule>

IIS Rewrite module - how to redirect maintaining path and querystring but adding an extra querystring parameter?

I've been banging my head against a brick wall attempting to get some IIS redirect rules to work. I've searched and read stuff here on Stack Overflow and on IIS.net but it just doesn't work at all.
I'm trying it on my local (real) IIS, I have the rewrite 2.0 module installed and have tried doing a repair install on it. I've done an iisreset at an admin cmd line more times than I care to mention.
In my hosts file I have set-up 127.0.0.1 for the URL my.test.com.
What I want to achieve is given a sub-domain URL redirect to the main domain URL with an extra querystring parameter whilst maintaining the existing path and querystring values if they exist.
I setup 3 rules as follows that are in the root folder of the website:
<system.webServer>
<rewrite>
<rules>
<rule name="PathAndQueryString" enabled="true" stopProcessing="true">
<match url="(/\w*)(\?\w*=\w*)([&\w*=\w*]*)" />
<action type="Redirect" url="https://www.test.com{R:1}{R:2}{R:3}&param=value" appendQueryString="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="my.test.com" />
</conditions>
</rule>
<rule name="Querstring" enabled="true" stopProcessing="true">
<match url="(\?\w*=\w*)([&\w*=\w*]*)" />
<action type="Redirect" url="https://www.test.com{R:1}{R:2}&param=value" appendQueryString="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="my.test.com" />
</conditions>
</rule>
<rule name="DomainOnly" enabled="true" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="https://www.test.com?param=value" appendQueryString="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="my.test.com" />
</conditions>
</rule>
</rules>
</rewrite>
</system.webServer>
Unfortunately despite testing the patterns and confirming the {R:x} captures are correct when I try to test this using Edge, Chrome, Firefox and IE for all of them IIS acts like the rules don't exist.
Tests:
https://my.test.com >> https://www.test.com?param=value
https://my.test.com/SomePath >> https://www.test.com/SomePath
https://my.test.com/SomePath?AParam=AValue >>
https://www.test.com/SomePath?AParam=AValue&param=value
None of the above tests work, they get served without a redirect.
I've also tried putting the condition pattern used as part of the Match URL pattern but it didn't work with that either; in fact I moved it to the condition after reading a Stack Overflow post which said when rules are in the root of the site it doesn't include the host, but still nothing works.
Any help would be much appreciated.
UPDATE 1: For the path and querystring rule tried removing the bolded part from that start of this pattern (/\w*). Didn't have any effect.
UPDATE 2: Tried enabling the extremely temperamental "Failed Request Tracing" functionality and when it is actually working it says that none of the Match URL patterns are matching, except for the 3rd rule which has now started kicking in and redirecting but is not maintaining the path and other querystring params for obvious reasons.
UPDATE 3: On Edge and IE none of the rules work at all. On Chrome and FF the domain only redirect appears to be working - however if I disable the domain only rule and restart IIS it acts as though the rule is still there - FFS give me strength this crap is really starting to boil my blood now - this should not be that damn difficult.
Before I give the answer, a shout out to Yuk Ding at Microsoft who responded to a copy of this post on the iis.net forums and provided most of the answer.
So here we go, if you want rewrite rules that redirects from 1 URL to another maintaining the path and querystring if they exist whilst at the same time adding on a hardcoded URL parameter with the appropriate & or ? then here are the rules you will need.
In the rules you are redirecting from my.test.com to www.test.com, but obviously you would need to replace those 2 URLs in the rules below as well as changed the hardcoded parameter "ExtraParam=SomeValue" to what you need.
<system.webServer>
<rewrite>
<rules>
<rule name="DomainOnly" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="my.test.com" />
<add input="{REQUEST_URI}" pattern="/.+" negate="true" />
</conditions>
<action type="Redirect" url="http://www.test.com?ExtraParam=SomeValue" redirectType="Temporary" />
</rule>
<rule name="PathOnly" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{REQUEST_URI}" pattern="/.+" />
<add input="{REQUEST_URI}" matchType="Pattern" pattern="\?.+" ignoreCase="true" negate="true" />
<add input="{HTTP_HOST}" pattern="my.test.com" />
</conditions>
<action type="Redirect" url="http://www.test.com{C:0}?ExtraParam=SomeValue" redirectType="Temporary" />
</rule>
<rule name="Querstring" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{REQUEST_URI}" pattern="/.+" />
<add input="{QUERY_STRING}" pattern=".*" />
<add input="{HTTP_HOST}" pattern="my.test.com" />
</conditions>
<action type="Redirect" url="http://www.test.com{C:0}&ExtraParam=SomeValue" appendQueryString="false" redirectType="Temporary" />
</rule>
</rules>
</rewrite>
In my eyes the URL Rewrite 2.0 module has a bug. The basic crux is that you cannot match anything in the main match pattern you have to do everything with conditions. Thus the fact that you can set a pattern for the match is completely pointless because if you try to match anything other than (.*), i.e. match anything like I originally did it won't do what you want and will effectively cause head-bashing-brick-wall-ness.
The even more annoying thing is that in IIS Manager the UI for this is very unhelpful. You can test your patterns - and I did - and they will work, but all it's really testing is the regEx pattern.
What we really need is a test at the rules level. In other words you enter a URL and it tells you whether any of the rules match and which conditions pass or fail. Yes you can get this information if you enabled the failing request tracing rules but it's all a long winded phaff to enable and works in a haphazard fashion (just try clearing out the old logs and watch as it then stops logged until you turn it off and on again) for something that should be trivial.

IIS URL Rewrite not working with query string

I thought this was pretty straightforward, but it refuses to work. The old URL is
http://www.site.com/?q=node/17
It needs to redirect to http://www.site.com. I don't need to worry about wildcards, this is the only query string parameter I need to worry about. The rule I wrote looks like
<rule name="Node17" patternSyntax="ExactMatch" stopProcessing="true">
<match url="http://www.site.com/?q=node/17" />
<action type="Redirect" url="http://www.site.com" appendQueryString="False" />
</rule>
I can test the pattern inside of IIS and it matches, but when I hit the URL in a browser it doesn't redirect. Any thoughts?
As described in Microsoft's documentation:
It is important to understand how certain parts of the URL string can
be accessed from a rewrite rule.
For an HTTP URL in this form:
http(s)://{host}:{port}/{path}?{querystring}
The {path} is matched against the pattern of the rule. The
{querystring} is available in the server variable called QUERY_STRING
and can be accessed by using a condition within a rule.
Rule conditions allow defining additional logic for rule evaluation...
Rule conditions are evaluated after the rule pattern match is successful.
In the URL you wanted to rewrite as a redirect, your {host} = "www.site.com", {path} = "" and {querystring} = "q=node/17". So the {path} part in the URL you wanted to redirect is actually empty, and the rule you used in your question was matched against it and did not match.
Your solution is indeed valid, so I'll quote it here:
<rule name="Node17" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="q=node/17" />
</conditions>
<action type="Redirect" url="http://www.example.com" appendQueryString="False" />
</rule>
Of course I figured it out soon after I posted. This does it, not really sure why the exactmatch wasn't working though.
<rule name="Node17" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="q=node/17" />
</conditions>
<action type="Redirect" url="http://www.site.com" appendQueryString="False" />
</rule>

Resources