Redirect all URLs to contain 'www' prefix - url

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.

Related

IIS url rewrite dont do anything

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>

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>

URL rewrite rule (iis): redirect ONLY www.example.com and www.example.com/default.aspx

I am trying to do a rewrite to redirect the following pages ONLY:
www.example.com/
www.example.com/default.aspx
I want to let all other requests pass through. I know I'm pretty close but I just can't seem to get it perfect. Thanks in advance for any & all help!
Here's what I have so far:
<rewrite>
<rules>
<rule name="Base Redirect" stopProcessing="true">
<match url="" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.example.com" ignoreCase="true" negate="false" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="shop.example.com" appendQueryString="true" />
</rule>
<rule name="Default.aspx Redirect" stopProcessing="true">
<match url="default.aspx" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.example.com" ignoreCase="true" negate="false" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="shop.example.com" appendQueryString="true" />
</rule>
</rules>
</rewrite>
Problem is it's sending anything containing default.aspx (/pages/default.aspx, etc) and we don't want to redirect anything but the root level default.aspx.
Felix
Try this (basically adding the "^" character to tell regex to only match when it is at the beginning:
<rule name="Base Redirect" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^$" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.example.com" />
</conditions>
<action type="Redirect" url="http://shop.example.com/" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="Default.aspx Redirect" stopProcessing="true">
<match url="^default.aspx" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.example.com" />
</conditions>
<action type="Redirect" url="http://shop.example.com" appendQueryString="true" redirectType="Permanent" />
</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