How can I exclude specific subdomains from redirect to HTTPS in web.config? - redirect

I'm working on an MVC project, and I have the following working rule in my web.config, but it's redirecting all the subdomains to https. What I would like to do is to exclude some specific subdomains. For example, I need to redirect "mywebsite.com" and "www.mywebsite.com", but not "test.mywebsite.com" or "beta.mywebsite.com". The excluded subdomain should remain as "http://test.mywebsite.com" and "http://beta.mywebsite.com" without being redirected to https. How can I do that? Here's my rule in web.config:
<rule name="Redirect to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{REQUEST_URI}" negate="true" pattern="^/\.well-known/pki-validation/(.*)$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="true" />
</rule>

I used to have a similar configuration had used a rule along this line:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="redirect to https2" stopProcessing="true">
<match url="^" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^subdomain\.example\.com$" negate="true" />
<add input="{HTTPS}" pattern="off" ignoreCase="false" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{URL}" redirectType="Found" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Related

IIS URL Rewrite not working when trying to redirect sub domain to domain with query string

I have what to me seems like a simple task with IIS URL Rewrite.
I need to redirect based on sub-domains to a main domain with a query string. For example
demo.domain.com needs to be redirected to www.domain.com/?key=demo
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="demo.domain.com" />
</conditions>
<action type="Redirect" url="www.domain.com/?key=demo" appendQueryString="false" redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
What's happen when you digit demo.domain.com ?
Try to change your code like this :
<rules>
<rule name="Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="^demo\.example\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/?key=demo" appendQueryString="false" redirectType="Found" />
</rule>
</rules>
Look at the changes in line "add input=" and "action type="

IIS url redirect from non-www to www not working

I have this rewriting rule:
<rewrite>
<rules>
<rule name="Force non-WWW and SSL" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.yourdomainname.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
It successfully redirects example.com to https://example.com, however, I need redirection to https://www.example.com, how can I do this?
try this ,just limit the domain or you want dynamic domain?
<rewrite>
<rules>
<rule name="Force non-WWW and SSL" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{HTTP_HOST}" negate="true" pattern="^www.(.*)$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
use the match all and the negate to redirect

In IIS, how can I forward all my URLs to a non-www, https, and no file extension without having too many redirects

I have gone through many similar questions in Stack Overflow, but I have not been able to find one that helps me to have all three of these re-direct rules work together.
I am trying to forward all of my URLs to this format:
https://example.com/page
I need the https, I don't want the www, and I don't want the html or aspx extensions to be visible. Here is what I have so far. Chrome says that I have too many redirects, how do I consolidate these rules?
<rewrite>
<rules>
<clear />
<rule name="RewriteHTML">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.html" />
</rule>
<rule name="Redirect www to non-www" enabled="true" stopProcessing="true">
<match url="(.*)\.html$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://example.com/{R:1}" redirectType="Permanent"/>
</rule>
<rule name="Redirect to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)\.html$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent"/>
</rule>
<rule name="RemoveExtension" stopProcessing="true">
<match url="(.*)\.html$" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
I finally figured it out after playing around with patterns and rule orders, and the stopProcessing command, etc. No more redirect loops! I actually figured out what I was asking and more.
Removes index.html from root domain
Redirects www to non-www
Removes trailing slashes
Makes the URL always lowercase
Redirects http:// to https://
Enables URL to work without .html extension
Redirects to URL without .html extension
My code:
<rules>
<!-- index.html Redirect -->
<rule name="index.html Redirect" enabled="true">
<match url="^(.*\/)*index\.html$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_METHOD}" negate="true" pattern="^POST$" />
</conditions>
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
<!-- Redirect WWW to non-WWW -->
<rule name="Redirect WWW to non-WWW" 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>
<!-- Remove Trailing Slash -->
<rule name="Remove Trailing Slash" enabled="true">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}" />
</rule>
<!-- Make URL Lower Case -->
<rule name="Make URL Lower Case" enabled="true">
<match url="[A-Z]" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{URL}}" />
</rule>
<!-- Redirect To HTTPS -->
<rule name="Redirect To HTTPS" enabled="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<!-- Redirect To URL With No Extension -->
<rule name="Redirect To URL With No Extension" enabled="true">
<match url="(.*)\.(html)" />
<action type="Redirect" url="{R:1}" />
</rule>
<!-- Rewrite URL For No Extension -->
<rule name="Rewrite URL For No Extension" enabled="true" stopProcessing="false">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:0}.html" />
</rule>
</rules>
Here is what the rules look like in the IIS URL Rewrite module:

IIS 7.5 - redirect url /index.php/* to /*

I have php a website set up under IIS 7 using cakePHP. It was running for quite a while using not-so-pretty urls of the form /index.php/[controller]/[action]/[etc]. Long enough for people to have bookmarks and Google to index everything. I've now fixed the web.config so that the urls that get used are of the from /[controller]/[action]/[etc], bypassing the index.php in the url (internally it all goes to the same place).
The problem is, that if you visit one of the links in the older format, the page still technically works, but css / images / etc aren't being loaded and it looks ugly. Is there a simple way to redirect from the /index.php/* format to the new /* format?
The following is my existing web.config performing the required rewrites:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect static resources" stopProcessing="true">
<match url="^(ico|img|css|files|js)(.*)$" />
<action type="Rewrite" url="app/webroot/{R:1}{R:2}" appendQueryString="false" />
</rule>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^$" ignoreCase="false" />
<action type="Rewrite" url="/" />
</rule>
<rule name="Imported Rule 3" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<action type="Rewrite" url="/{R:1}" />
</rule>
<rule name="Imported Rule 4" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I worked out what I was missing. In the redirect rule, I match on /index.php/* but I didn't realize you can add the matched string into the destination using $0, $1, $2, etc. Also of note, the exactDestination="true" is necessary, otherwise it redirected /index.php/[controller]/[action]/[etc] to /[controller]/[action]/[etc]/index.php/[controller]/[action]/[etc]
I just had to add the following section to the top of the web.config (in the <system.webserver> section, before the <rewrite> section:
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Permanent">
<add wildcard="/index.php/*" destination="/$0" />
</httpRedirect>

Redirection using web.config on wordpress website

I have issue about redirection using web.config . It is word-press website hosted on godaddy.com. Server IIS 7.0 It is window server.
I want redirect from old html page to new wordpress page.
Example:
http://www.dakshadesign.com/Ecommerce_Web_Design.html to http://www.dakshadesign.com/ecommerce-website-design-web-development-company-india/
Resloved now
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Redirect to dakshadesign.com" stopProcessing="true">
<match url="^Ecommerce_Web_Design.html$" />
<action type="Redirect" url="http://www.dakshadesign.com/ecommerce-website-design-web-development-company-india" appendQueryString="false" />
</rule>
<rule name="wordpress" patternSyntax="Wildcard">
<match url="*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>