I want to add customHeaders to all my routes. But only to actual paths in my domain and not files.
<location path="???">
Headers should be added to:
domain.com
domain.com/dashboard
but not to
domain.com/someimage.jpg
You could use IIS rewrite module to write a custom outbound rule. It already has OOTB condition to check if a URL is a file. Something like below, this question is similar:
<rewrite>
<outboundRules>
<rule name="Set custom HTTP response header">
<match serverVariable="Custom header" pattern=".*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" value="Your value"/>
</rule>
</outboundRules>
</rewrite>
Related
I would like to set IIS Redirection for a URL based on HTTP_COOKIE
cookie-name is Token and value is dynamic.
I tried below rules but each of them gave me errors -
<rule name="redirect based by cookie" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_COOKIE}" pattern="_xx=HCjdskfds==" />
<add input="{HTTP_HOST}" pattern="xxx.yyy.com" />
</conditions>
<action type="Redirect" url="http://zzz.kkk.com/{R:0}" />
</rule>
<rule name="Route Base On Cookie" stopProcessing="true">
<match url="^(.*)" />
<conditions>
<add input="{HTTP_COOKIE}" pattern="foo=(.*?);" />
</conditions>
<action type="Rewrite" url="http://{C:1}/{R:0}" />
</rule>
both of them above rule will give error on every url on my domain
What i'm trying here is to redirect if that page doesn't contain cookie Token to error page/ Login page
Thanks in advance........
Goal: Redirect user if a cookie is NOT present in their request.
Solution:
Add to web.config for IIS at root of project
match every url
Assumes cookie is CookieName=any_value
Matching pattern uses regex to check cookie name with any value
use negate="true" as a not operator; if cookie is NOT found
redirectType is optional. Default is 301, Found is 302.
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect if Cookie is present" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_COOKIE}" pattern="CookieName=*" negate="true" />
</conditions>
<action type="Redirect" url="https://www.my-domain.com/login" redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
This rule is basically a wildcard for a cookie with the name CookieName so that if a cookie with that name has any value at all (it exists), then you wont be redirected. If the cookie is NOT found, you will be redirected.
One caveat is that there's no way to identify an HttpOnly cookie vs a browser-set cookie... :(
Sources:
URL Rewrite Module Configuration
What I'm trying to do:
I own a domain, let's say blahblahblah.com
On the server, it points to :80 and :443 properly - this is working
I want to remap so that if a user visits blahblahblah.com/thing1 then it maps to localhost:5000
Similarly, I also want to remap so that if a user visits blahblahblah.com/thing2 then it maps to another server and port on my local network (not exposed to the internet)
I essentially want to use my webserver to communicate with other servers on my network and other ports through "sub directories" or whatever they are called in this instance (site/subdir, site2/subdir). I've been trying to solve this issue for days! I've installed ARR (and enabled the Proxy) and the URL Rewrite modules on my IIS.
I've attempted god knows how many different rewrite templates on SO. I can get partial success, in which the route /thingx/ resolves, but all the resources are referencing blahblahblah.com and (appropriately) can't find the resources for the site at that address.
Current rewrite (resolves some resources at blahblahblah.com/thing1, but others are misrouted):
<rewrite>
<rules>
<rule name="Thing1" enabled="true">
<match url="(thing1*)" />
<action type="Rewrite" url="http://localhost:5000/{R:0}" />
</rule>
</rules>
</rewrite>
EDIT: I've updated my rules to the below, as per this article from Microsoft Docs: Reverse Proxy with URL Rewrite v2 and Application Request Routing . These rewrites supposedly do exactly what I need, but the result is not the same in my case, and I still have no clue why.
<rules>
<rule name="Thing1" enabled="true">
<match url="^thing1/(.*)" />
<action type="Rewrite" url="https://localhost:5000/{R:0}" />
</rule>
</rules>
<outboundRules>
<rule name="Add application prefix" preCondition="IsHTML">
<match filterByTags="A" pattern="^/(.*)" />
<conditions>
<add input="{URL}" pattern="^/(thing1)/.*" />
</conditions>
<action type="Rewrite" value="/{C:1}/{R:1}" />
</rule>
<preConditions>
<preCondition name="IsHTML">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
More Notes:
localhost:5000 works as expected
localhost/thing1 resolves in 404 Not Found
blahblahblah.com/thing1 resolves in pulling in the default HTML page, but none of the assets (javascript, css, etc), they return 404 Not Found.
EDIT: It may be important to note that these rules have been written into the default site (bound to :80, :443) on IIS' web.config. Writing these rules in C:\Windows\System32\inetsrv\config\applicationHost.config results in a 500.
Any feedback on what I'm doing wrong?
Part of this was due to the way SPAs are handled (# and history mode), but I ended up solving with the below webconfig for IIS, which allows me to nest everything under a /foobar endpoint:
<rewrite>
<rules>
<rule name="Subdirectory Index" stopProcessing="true">
<match url="^foobar/index\.html$" ignoreCase="false" />
<action type="None" />
</rule>
<rule name="Subdirectory Routes" stopProcessing="true">
<match url="." ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="/foobar/index.html" />
</rule>
</rules>
</rewrite>
I'm relatively new to IIS Rewrite Rules so forgive me if this is not actually feasible in IIS Rewrite.
I'm trying to setup a 'fallback' route for some static content on a site. I always have the root content, but not necessarily the slug-path. E.g.
http://example.com/some-slug/foo.js
If /some-slug/foo.js doesnt exist, I want to fall back to /foo.js on the root of the site, and serve this as the response.
Is this achievable in IIS re-write?
Yes it is achievable
<rewrite>
<rules>
<rule name="rewritewithfallback">
<match url="some-slug/(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}" />
</rule>
</rules>
</rewrite>
I have a widows machine hosted by goddady and I need to create a rule to forward any request with a URL pointing to a non-existing file to be sent to the index.php.
Usually I would do that with the .htaccess but its different in windows hosts.
I found the answer.
I had to put a xml file named 'web.conf' in the root directory, it works in the same way .htaccess does.
I took the rules from ZendFramework Site
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^.*$" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}"
matchType="IsFile" pattern=""
ignoreCase="false" />
<add input="{REQUEST_FILENAME}"
matchType="IsDirectory"
pattern=""
ignoreCase="false" />
</conditions>
<action type="None" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^.*$" />
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
This rules will make the server check if the file exsists and if it doesn't then the request is forwarded to index.php
I want to use web.config and IIS's urlrewrite to rewrite all requests to a certain directory to the root, for example:
From:
mydomain.com/directory/test.php
To:
mydomain.com/test.php
and
From:
mydomain.com/directory/test/test.php
To:
mydomain.com/test/test.php
All parameters, etc should be passed as well. Any idea how to do this using web.config?
I had a requirement for this a while back and found this:
Using Url rewrite to "delete" a folder
There's a small error in the rule which was corrected later by the poster. Here's a copy with the correction should the link dry up (I also made it a bit more generic for your requirements):
<rewrite>
<rules>
<rule name="RemoveDirectory" stopProcessing="true">
<match url="^directory$|^directory/(.*)$" />
<conditions>
</conditions>
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="RewriteToFile">
<match url="^(?!directory/)(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="directory/{R:1}" />
</rule>
</rules>
</rewrite>
Just search and replace directory with your directory name.