IIS url rewrite dont do anything - url

I would like to url redirect from:
https://media.domain.com/link to file.ext
to:
https://media.domain.com:8080/link to file.ext
I have made this:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
<directoryBrowse enabled="true" showFlags="Date, Time, Size, Extension, LongDate" />
<rewrite>
<rules>
<rule name="Redirect to port 8080" enabled="true" stopProcessing="true">
<match url="(.*)/(.*)/(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
</conditions>
<action type="Redirect" url="http://{R:2}:8080/{R:3}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
But when I enter https://media.domain.com/link to file.ext IIS just gives me a 404 error, and it does not redirect.
IIS simply ignores my url rewrite..
Any thoughts?

you could try below rule:
<rule name="domian rule" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^media.domain.com$" />
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Redirect" url="https://media.domain.com:8080/{R:1}" />
</rule>

Related

How to write a general rule to redirect all user urls to one valid Url by using asp.net mvc 5?

I want to redirect if user type some Urls in the browser. My application is developed in Asp.net MVC 5.
please see the possible user inputs in the browsers
User can enter
1). test-systems.co.uk
2). www.test-systems.co.uk
3). https://test-systems.co.uk
4). https://www.test-systems.co.uk
5). http://www.test-systems.co.uk
All above url should be redirected to
https://www.test-systems.co.uk
I have tried the above rule but all URLs are not working as expected
This is my web.config rule detail
<rewrite>
<rules>
<clear />
<rule name="Rewrite subdomain" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" type="Pattern" pattern="^(www.)?\.test-systems.co.uk:4005$" />
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{URL}" pattern="(.*)" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="http to https"
enabled="true"
patternSyntax="Wildcard"
stopProcessing="true">
<match url="*" />
<conditions
logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off"
/>
</conditions>
<action type="Redirect"
url="https://{HTTP_HOST{REQUEST_URI}" redirectType="Found"
/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Redirect all URLs to contain 'www' prefix

I want to redirect all traffic from
https://url
to
https://www.url
How can I write the redirect url?:
<rewrite>
<rules>
<rule name="Add www prefix">
<match url="(.*)" ignoreCase="true" />
<action type="Redirect" url="what to put here?" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
have you tried doing like that
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Add www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.YOURWEBSITE.com" negate="true" />
</conditions>
<action type="Redirect" url="http://www.YOURWEBSITE.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
You can find more here
Try something like this :
<rules>
<rule name="www redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://www.{HTTP_HOST}/{R:1}" />
</rule>
</rules>
Adjust that to your needs but i think it works.

IIS rewrite module canonical URL with two domains

I have a webserver and I have two domains with name http://example.mx and http://example.com.mx both are pointing to the same webserver and root folder, I can use my website with both domains and for the moment I can use www. as well but this is what I want to remove, I did it and I got this web.config file
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<remove name="CGI-exe" />
<add name="CGI-exe" path="*.exe" verb="*" modules="CgiModule" resourceType="File" requireAccess="Script" allowPathInfo="true" />
</handlers>
<rewrite>
<rules>
<rule name="CanonicalHostNameRule2" enabled="false">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com\.mx$" negate="true" />
</conditions>
<action type="Redirect" url="http://example.com.mx/{R:1}" />
</rule>
<rule name="CanonicalHostNameRule1" enabled="false">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.mx$" negate="true" />
</conditions>
<action type="Redirect" url="http://example.mx/{R:1}" />
</rule>
<rule name="http a https" enabled="false" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
But the problem is the I go to http://example.mx it sends me to http://example.com.mx and it suppose to works like this:
If I go to http://www.example.mx redirects to http://example.mx
If I go to http://www.example.com.mx redirects to http://example.com.mx
but right now if I go to http://www.example.mx it redirects to http://example.com.mx and I dont want this, What I need to change in the pattern?
You should use this rules:
<rule name="CanonicalHostNameRule2">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.example\.com\.mx$"/>
</conditions>
<action type="Redirect" url="http://example.com.mx/{R:1}" />
</rule>
<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.example\.mx$" />
</conditions>
<action type="Redirect" url="http://example.mx/{R:1}" />
</rule>

IIS URL Rewrite if link does not match

<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mywebsite.com$" />
</conditions>
<action type="Redirect" url="http://mywebsite.com/"
redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
That code works:
if it's website.com, it links you to another one
but, I want it so if it's NOT website.com, it links you

URL Rewrite and Web.config

i need to redirect from
www.domain.com to https://domain.com
http://www.domain.com to https://domain.com
http://domain.com to https://domain.com
I have the following lines of code and it works fine
<system.webServer>
<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>
</system.webServer>
i take it from here
I want also rediredt from
https://www.domain.com to https://domain.com
i add the following lines
<rule name="Remove WWW" stopProcessing="true">
<match url="^https://www.(.*)" />
<action type="Redirect" url="http://{R:1}" />
</rule>
but it no redirect from https://www.domain.com to `https://domain.com
why?
You are the conditions snippet in between your <match> and <action> tags for https redirection.
Try this:
<rule name="Remove WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^https://www.domain\.com$" />
</conditions>
<action type="Redirect" url="http://{R:1}" />
</rule>

Resources