IIS 7 rewrite rule - redirect based on presence of querystring - redirect

Is it possible to redirect using web.config based on the presence of a querystring in the initially requested URL? I'm not sure what to include in the the conditions. I'm a beginner at working with rewrite/redirect rules in web.config and would like to learn more about syntax, parameters etc.
I'm trying to do something like this:
<rewrite>
<rules>
<rule name="Restricted Folder with Querystring" stopProcessing="true">
<match url="^test folder/(.*)" ignoreCase="false" />
<conditions>
<!--redirect to a certain page if a certain querystring is present -->
</conditions>
<action type="Redirect" redirectType="Permanent" url="/test folder/{R:1}" />
</rule>
<rule name="Restricted Folder without Querystring" stopProcessing="true">
<match url="^test folder/(.*)" ignoreCase="false" />
<conditions>
<!--redirect to another page if querystring is not present -->
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://www.whatever.com/page.asp?url={R:1}" />
</rule>
</rules>
</rewrite>

deeholzman, I believe for your Condition - Add Input example you want to use "pattern" and not "matchType". EX:
<add input="{QUERY_STRING}" pattern="^whateverpattern" />
or
<add input="{QUERY_STRING}" pattern="^whateverpattern" negate="true" />

I found the answer in the URL Rewrite Module Configuration Reference. In the rewrite section of web.config in whatever folder you wanted to perform the redirect from, it would go something like this:
<rules>
<rule name="Restricted Folder Gimbal" stopProcessing="false">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{QUERY_STRING}" pattern="^whateverpattern" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="http://www.whatever.com/file.html" />
</rule>
</rules>
For a querystring not present, you could add the 'negate' parameter to the condition:
<add input="{QUERY_STRING}" pattern="^whateverpattern" negate="true" />
The the URL Rewrite Module Configuration Reference is a very handy reference that unfortunately took me longer than expected to find.

Related

IIS rewrite rule not passing parameters

I have redirects currently set up to go from oldsite.com to newsite.com using a rewrite map. They work except when a parameter is passed through the URL, ex. http://oldsite.com?utm_source=go.wayne.edu&utm_medium=direct&utm_campaign=quick-access&utm_content=, still goes to the old page instead of redirecting to the new page with the parameters, ex. http://newsite.com?utm_source=go.wayne.edu&utm_medium=direct&utm_campaign=quick-access&utm_content=.
Here's my code:
<rule name="Redirects to New Site">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{RelaunchRedirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="http://newsite.com/{C:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
Still very new to all of this and I can't figure out why the parameters are not passing. I've seen some similar cases on here but haven't been able to get those solutions to work for me.
Any ideas on what I can do to make this work?
You can try to use this URL Rewrite rule:
<rewrite>
<rules>
<rule name="Redirects to New Site" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
</conditions>
<action type="Redirect" url="http://newsite.com{URL}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Feel free to let me know if there are any questions.

IIS redirect rule not working on specific folder

I have the following file paths:
C:\projects\myproject\mysite\Content\Products\123\a.jpg
and
C:\projects\myproject\mysite\Content\img\banner\webp\b.webp
C:\projects\myproject\mysite is hosted on IIS and in my web.config I have a working rewrite rules like those:
<rewrite>
<rules>
<rule name="REDIRECT_WEBP_PRODUCTS" stopProcessing="false">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="(.*)/webp/(.*)" />
</conditions>
<action type="Redirect" appendQueryString="false" url="http://localhost/mysite/content/img/logo.svg" redirectType="Permanent" />
</rule>
<rule name="REDIRECT_IMAGE_PRODUCTS" stopProcessing="false">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="(.*)/products/(.*)" />
</conditions>
<action type="Redirect" appendQueryString="false" url="http://localhost/mysite/content/img/logo.svg" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
So my images under Content\Products\123\ are redirected to logo.svg as expected but REDIRECT_WEBP_PRODUCTS doesn't work.
.webp files extension is handled correctly (mime type support is ok), so the problem isn't related to the file extension. (tried with a .jpg under webp/ folder too).
It seems like redirection is working only for request for files under Content\Products...
Long story short:
<img class="article__image" data-src="/mysite/content/products/123/a.jpg" alt="a" src="/lordgun/content/products/123/a.jpg">
works
but
<img class="banner__img" src="/mysite/content/img/banner/webp/b.webp" alt="b">
not.
I've managed to solve this by putting a web.config with a specific rewrite rule directly in webp/ folder.

Redirect url with a hashcode to another with same hashcode in IIS10

I need to redirect a url with a variable hashcode to another url with that same hashcode in IIS10 (Windows Server 2019).
Example:
https://www.example.com/hello/sd54effg1g5s11d5111dwds21fds2f1ffd
Needs to redirect to:
https://subdomain.example.com/hello/sd54effg1g5s11d5111dwds21fds2f1ffd
At the moment i have this as a rule in the web.config:
<rule name="rulename" stopProcessing="true">
<match url="^hello/[a-zA-Z0-9]+$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(https:\/\/www\.)example.com$" />
</conditions>
<action type="Redirect" url="https://subdomain.{HTTP_HOST}/{R:1}" />
</rule>
Firstly, the {HTTP_HOST} is will not match the https part in the url. So you should use ^www.example.com$ instead of ^(https:\/\/www\.)example.com$.
Besides, you should use https://subdomain.example.com/{R:1} instead of the https://subdomain.{HTTP_HOST}/{R:1} to achieve your requirement.
Details ,you could refer to below url rewrite rule:
<rule name="rulename" stopProcessing="true">
<match url="^hello/[a-zA-Z0-9]+$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://subdomain.example.com/{R:0}" />
</rule>

URL ReWrite with un-named parameter

I would like this URL
http://alpha.Mysite.io/Car/Details/{4f2a95ed-3582-4486-8ef6-8a8e6731161f}
to redirect to
http://alpha.Mysite.io/Car/index.htm?Action=Details&guid={4f2a95ed-3582-4486-8ef6-8a8e6731161f}
The GUID parameter ({4f2a95ed-3582-4486-8ef6-8a8e6731161f}) is differenet each time so needs to be passed to the redirected URL.
I have the basic redirect working using a ReWriteMap, however as soon as I add the nameless GUID paramter I get a 404 instead
Here's my section from webconfig
<rewrite>
<rules>
<rule name="Rewrite rule1 for carAPI">
<match url=".*" />
<conditions>
<add input="{carAPI:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="true" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="carAPI">
<add key="/car/details/" value="/car/index.asp?Action=Details" />
</rewriteMap>
</rewriteMaps>
</rewrite>
In your case, you do not need rewrite map. You can achieve everything with single rule:
<rule name="Rewrite rule1 for carAPI" enabled="true" stopProcessing="true">
<match url="^Car/Details/([0-9A-Fa-f]{8}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{12})" />
<action type="Rewrite" url="/Car/index.htm?Action=Details&guid={R:1}" appendQueryString="false" />
</rule>
This rule will match urls like Car/Details/{any valid .net guid} and rewrite them into Car/index.htm?Action=Details&guid={guid}

iis url redirect http to non www https

i need to redirect from
www.domain.de to https://domain.de
-works
http://www.domain.de to https://domain.de
-works
http://domain.de to https://domain.de
-does not work
rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^www\.(.+)$" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
I think this will work for you, the search pattern has the optional www and redirects using the back reference C:2, the rule has a condition to only run against non https.
This is the pattern:
"^(www\.)?(.*)$"
where:
{C:0} - www.domain.de
{C:1} - www.
{C:2} - domain.de
Here's the rule in full:
<rewrite>
<rules>
<rule name="SecureRedirect" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
The accepted answer doesn't handle the special case https://www.domain.de.
These rules do the complete job:
<rewrite>
<rules>
<rule name="Redirect to HTTPS without www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Special case for HTTPS with www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^ON$" />
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
If you want to redirect www to non www:
Add DNS entry for www.yourdomain.com to refer to your server's public IP
Then you need to Edit Bindings of your website and "Add Binding" www.yourdomain.com
Add a rewrite rule to your website using iis:
<rule name="Remove WWW" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{CACHE_URL}" pattern="*://www.*" />
</conditions>
<action type="Redirect" url="{C:1}://{C:2}" redirectType="Permanent" />
</rule>
Reference: http://madskristensen.net/post/url-rewrite-and-the-www-subdomain
If you want something more flexible than for your three examples, change your HTTP_HOST pattern to : \w+\.\w+$. That would work for all three examples plus anything else, like subdomain.abcdef.domain.de.
If you use this regex either encase it in parenthesis or change C:1 to C:0 in your action.
This is what worked for me:
<rule name="NameRule1" stopProcessing="true" enabled="true" >
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://example.com/{R:1}" />
</rule>
I got it from: https://medium.com/iis-and-windows-server/redirect-url-from-www-to-non-www-in-iis7-5-4d2909b9704
Redirecting https://www.example.com to https://example.com I had to do 2 steps
Select the website, click "URL Rewrite" -> Click "Add Rule" -> Choose "Cononcial domain name" -> select your domain (without the www) -> make sure rule is at the top -> highlight the rule and click "Edit" -> and update "Redirect URL" at the end of the rule to include the 's' - https://example.com/{R:1}
I had to also create a new website with the domain www.example.com, new app pool and select the SSL cert and point it to a folder containing the following (for some reason StackOverflow wont display the code I pasted below for the rewrite rule but just google IIS rewrite rule)
After doing both steps only then does it work as desired
Tried many of solutions but below works for me:
Right click on Domain and Add a Site bindings, add one more domain with www:
2. Restart services. it should work.
These rewrite rules matches the following URL's:
www.example.com
http://www.example.com
https://www.example.com
They will all redirect to: https://example.com
These rewrite rules may redirect twice because of the separate rules. (I'm a newbie with regex)
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTPS" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
<rule name="WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="https://example.com{PATH_INFO}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>