Simple rewrite from root domain to subdirectory IIS7 - iis-7

I know the basics of how to rewrite but cant seem to find a rule that works the way I want it too.
Any help would be greatly appreciated :o)
<rule name="RedirectRule" stopProcessing="true" enabled="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)?blah.com$" />
</conditions>
<action type="Rewrite" url="blog/{R:1}" />
</rule>
Basically I want it to only rewrite:
www.blah.com --> www.blah.com/blog
blah.com --> www.blah.com/blog
www.blah.com/blog --> do nothing.
www.blah.com/any-thing-else --> do nothing.

What about adding in a second condition to check PATH_INFO? Seeing as you only want to re-write when this is not specified...
<add input="{PATH_INFO}" pattern="^$" negate="true" />
The checks that PATH_INFO 'Does Not Match the Pattern' ^$ which is an empty string.
I'm not able to test this right now, so it might need tweaking...

Related

URL Rewrite with Relative URLs

I most likely might be missing something. I found this URL Rewrite example on this great post: http://marisks.net/2017/05/14/changing-static-resource-urls-to-cdn-urls-with-url-rewrite/
My setup is identical. I have relative URLs in my code to which I want to rewrite them on-the-fly to point to my CDN.
<outboundRules>
<clear />
<rule name="CDN" preCondition="CheckHTML" enabled="false" stopProcessing="true">
<match filterByTags="Img, Link, Script, CustomTags" customTags="Video" pattern="(^(?!www\.|(?:http|ftp)s?:\/\/|[A-Za-z]:\\|\/\/).*\.(jpg|jpeg|png|js|css|mp4).*)" />
<action type="Rewrite" value="http://cdn.example.com{R:1}" />
<conditions logicalGrouping="MatchAll">
</conditions>
</rule>
<preConditions>
<preCondition name="CheckHTML">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
<customTags>
<tags name="Video">
<tag name="source" attribute="src" />
</tags>
</customTags>
</outboundRules>
This works great. Except I need to fine-tune the rewrite to specific folders.
I know this sounds simple, but when I look at the Regex in the above script - I fail to see where I can place any sort of restriction on the words specifically in my relative URLs. Obviously I need to work harder at Regex. But any help is appreciated.
I need to only capture relative links with certain folders like /media or /css ("/media/media.mp4" or "/css/base.css").
I believe I fixed the issue. At least, it properly works for the folders I've set to rewrite and ignores the folders I have not specified. I'll do some more testing, but this works for now.
<rule name="CDN" preCondition="CheckHTML" enabled="true" stopProcessing="true">
<match filterByTags="Img, Link, Script, CustomTags" customTags="Video" pattern="(^(\/(media|css)+)*\/.*)" />
<action type="Rewrite" value="http://cdn.example.com{R:0}" />
<conditions logicalGrouping="MatchAll">
</conditions>
</rule>
The PreConditions and Tags are the same throughout.
Updated the Regex:
(^\/media\/.*)|(^\/css\/.*)

web.config redirecting non www to www with one route exception

Hello Im having trouble configuring my web.config file with a redirect rule.
My redirect rule now is
<rule name="Redirects to www.example.com" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" />
Now I want to exclude url https://example.com/some-url (and all url-s under this one) from the rule mentioned above. This rule should not redirect https://example.com/some-url to https://www.example.com/some-url.
So how do I exclude this url from the redirect rule.
Can't you just add a condition like this (note: now edited):
<add input="{REQUEST_URI}" pattern="/some-url/(.*)" negate="true" />
to the list of conditions?
Edit: and set the conditions to "MatchAll" rather than "MatchAny"
Not tested, but seems like that may work?
(Note: Edited answer as First rule was definitely wrong; current version may work but still untested)

IIS URL Rewrite - ignore if URL contains

I have the following URL rewrite setup inside web.config working as I would like.
<rule name="Product Rewrite" stopProcessing="true">
<match url="^products/([^$]+)/([^$]+)" />
<action type="Rewrite" url="products?durl={R:1}&purl={R:2}" />
</rule>
Now I need to exclude any URLs that contain or end with /action/edit i.e. products/action/edit
I understand that I will need a conditions block, but I am not sure what to write.
Any help would be greatly appreciated.
Add a condition like below, negate true ensures it matches everything except action/edit
<conditions>
<add input="{URL}" negate="true" pattern="action/edit" />
</conditions>

setting rule in IIS url rewrite module

I looking for help making URL rewrite rule for redirecting all urls that start with
http://localhost:13080/saCore/ws/messagebroker[Here something that changes]
to
http://localhost:13080/saCore/ws/messagebroker/MessageBroker.asmx[Here something that changes]
I just to need to push MessageBroker.asmx in the middle of the path after messagebroker part.
Thanks for help.
This should do the job, you can add it directly to applicationHost.config or use IIS Manager to create the rule:
<rule name="RedirectBroker" stopProcessing="true">
<match url="^saCore/ws/messagebroker(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^localhost$" />
<add input="{SERVER_PORT}" pattern="^13080$" />
</conditions>
<action type="Rewrite" url="/saCore/ws/messagebroker/MessageBroker.asmx{R:1}" appendQueryString="true" />
</rule>

IIS 7 URL Rewrite

How do I rewrite everything after the question mark (?) as one parameter?
For example, I have a url as: http://www.example.com?abcdefg/test/module?wiating4request
Notice after the first question mark we have another in the query string. I basically need to post to that url and I cannot modify the url so I need to make do with what is provided.
I saw something similar here: How can I use mod_rewrite to remove everything after the ? (question mark) in a URL?
Keep in mind this is for IIS 7.
Any ideas?
It's possible to do this with a rewrite rule in case you only want to match URL's with just one question mark too many, like your example. You can then use this rule:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Clean extra question mark from query string" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="^(.+?)(\?(.+))*$" />
</conditions>
<action type="Rewrite" url="/{URL}?{C:1}&{C:3}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
If you want to match an unlimited number of too many question marks I think you will need to revert to a custom rewrite provider as detailed in the linked article. You might then end up with something like:
<rule name="Clean extra question mark from query string" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="^$" negate="true" />
</conditions>
<action type="Rewrite" url="/{URL}?{ReplaceProvider:{QUERY_STRING}}" appendQueryString="false" />
</rule>

Resources