Directory Specific url rewriting in IIS ColdFusion - mod-rewrite

I have site
http://example.com/school/student.cfm?id=100
Also, i have another link http://example.com/collage/student.cfm?id=200
I want to write a URL rewrite rule which always re-write the http://example.com/school/student.cfm?id=100
to http://example.com/school/student/100
and
http://example.com/collage/student.cfm?id=200
to http://example.com/collage/student/200
I have written rule like this but that will always use 1 directory, But I want this to be working for all directories.
<rule name="Redirect to school with ONE parameter" stopProcessing="true">
<match url="^.*/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="school/student.cfm?id_Name={R:1}" />
</rule>

There are many ways to solve this. The range of solution goes from - only rewriting school and collage - to rewriting any path that ends with student - to using a generic pattern that rewrites everything.
To give you an idea how to do that with IIS' regexp:
<match url="^(collage|school)/student/([0-9]+)/?$" />
<action type="Rewrite" url="{R:1}/student.cfm?id={R:2}" />
<match url="^([^/]+)/student/([0-9]+)/?$" />
<action type="Rewrite" url="{R:1}/student.cfm?id={R:2}" />
<match url="^([^/]+)/([^/]+)/([0-9]+)/?$" />
<action type="Rewrite" url="{R:1}/{R:2}.cfm?id={R:3}" />

Related

IIS 10 - URL rewrite rules to block direct requests to servername except from SOAPUI

Window 2016 / IIS 10.
I want to block all requests that are using the servername:portnumber/service and enforce the use of DNS-aliases. Problem at the moment seems to be that when the rule "Allow SOAPUI" matches it does not stop processing and therefore the last one kicks in and blocks SOAPUI
<rule name="Allow SOAPUI" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="dsttst100*" />
<add input="{HTTP_USER_AGENT}" pattern="*SOAPUI*" negate="true" />
</conditions>
<action type="Rewrite" url="http://redirect.to.what" />
</rule>
<rule name="Only allow requests from loadbalancer" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{REMOTE_ADDR}" pattern="111.22.55.11" negate="true" />
</conditions>
<action type="CustomResponse" statusCode="403" subStatusCode="6" statusReason="Only allowed from IISAR01 (use DNS) or using SOAPUI" statusDescription="Use dns-alias" />
</rule>
While trying do describe my issue I did figure out the solution - at least one possible solution. The reason for the second rule being triggered is that whenever a request with servername:port and SOAPUI user agent was triggered it did not match the first rule...since it was SOAPUI. Solution was to create a second rule with action type none if servername:portnumber AND SOAPUI.
<rule name="Servername - allow SOAPUI" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="dsttst100*" />
<add input="{HTTP_USER_AGENT}" pattern="*SOAPUI*" />
</conditions>
<action type="None" />
</rule>
This will then prevent the last rule to be processed.

When a dot exist in the middle of the URL, the rewriter works wrong

I have a project in asp net core 2.0.
I Have stablished the next Rule in the web.config:
<rewrite>
<rules>
<rule name="wwwroot" >
<match url="([\S]+[.](js|css|png|gif|jpg|jpeg))" />
<action type="Rewrite" url="wwwroot/{R:1}" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
When i acces to the url: www.foobar.com/lib/chart.js/dist/Chart.js for some reason the iis try to finde Chart.js in the path: wwwroot/lib/chart.js ignoring the last part of the url (/dist/Chart.js) when im expecting to take wwwroot/lib/chart.js/dist/Chart.js
Try to modify your regexp like this: ([\S]+[.](js|css|png|gif|jpg|jpeg))$ it should consider file extension if they at the end of your url
EDIT
To your rule you should exclude urls starting from wwwroot
<rule name="wwwroot" >
<match url="([\S]+[.](js|css|png|gif|jpg|jpeg))" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" negate="true" pattern="^/wwwroot" ignoreCase="true" />
</conditions>
<action type="Redirect" url="wwwroot/{R:1}" logRewrittenUrl="true" />
</rule>

Configure correct routing for folder and file with the same naming on IIS

I have site hosted IIS with hidden extentions
using this rule
<rule name="Hide .html ext">
<match ignoreCase="true" url="^(.*)"/>
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
<add input="{REQUEST_FILENAME}.html" matchType="IsFile"/>
</conditions>
<action type="Rewrite" url="{R:0}.html"/>
</rule>
<rule name="Redirecting .html ext" stopProcessing="true">
<match url="^(.*).html"/>
<conditions logicalGrouping="MatchAny">
<add input="{URL}" pattern="(.*).html"/>
</conditions>
<action type="Redirect" url="{R:1}"/>
</rule>
The main problem is that i have file that without extension has same naming that one of my folder
for example
wwwroot/page.html
wwwroot/page/page.html
When this rule applying server return
403 - Forbidden on when i call www.test.com/page
and all works fine when i call www.test.com/page/page
Is it possible to configure url rewrite module to correct work with both of this path?
You can do some workaround for that problem. If you will delete this condition: <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
Then your HTML page will work for this url www.test.com/page and www.test.com/page/page but will not for www.test.com/page/ (it will return forbidden).
If page.html is single exception in your system and you will not have the same collisions, then you can also do in another way, create the additional rule for your url. Just add this rule before Hide .html ext rule:
<rule name="Exception for page" stopProcessing="true">
<match url="^(page)(\/?)$" />
<action type="Rewrite" url="{R:1}.html" />
</rule>
In this case, all URLs will work correctly:
www.test.com/page
www.test.com/page/
www.test.com/page/page

Redirect domain.com/abcd to domain.com/mypage.aspx/abcd

I'm trying to create an IIS redirect from
domain.com/abcd
to
domain.com/mypage.aspx/abcd
abcd can be any set of characters or numbers (abcd, ab, ab4c, etc..)
mypage.aspx will always be mypage.aspx
I find many ways of doing this backwards but for some reason I can't get this to work. Any suggestion?
Thanks,
I don't understand why this question has been unanswered for so long as it's quite simple to do:
<rewrite>
<rules>
<rule name="Redirect to mypage.aspx" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="domain\.com$" />
<add input="{REQUEST_URI}" pattern="^/mypage\.aspx/" negate="true"/>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/mypage.aspx/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Hope this helps.
EDIT: Made it dynamic, also added exception for existing files / directories, otherwise static content would not work anymore.

IIS8 rewrite to force https except for a certain path

My site is asp.net.
I have a wordpress blog in the blog folder and everything works great.
My main web.config has a working rule that forces https on everything.
I have successfully added a rule in the web.config in the blog folder not to redirect to https.
My problem is that I want to exclude the wp-content/uploads/* folder from that rule.
i.e. I want the images in the upload folder to be allowed to load on https too.
(My reason is that I refer to them in my main site and the way it is, I get security messages that I have insecure content on my https pages)
My code is:
<rule name="Remove https" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_URI}" pattern="^wp-content/uploads/.*" negate="true" />
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/blog/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
What am I doing wrong?
Thanks
Your code in problem dear plz try below code can this help you.
Something Wrong in your pattern of "^wp-content/uploads/.*" you try below code can this help you.
<rule name="Remove https" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_URI}" pattern="^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*)" negate="true" />
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/blog/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>

Resources