My IIS redirect rule is not working via web.config - redirect

I have a redirect rule that I am trying to create using web.config. I am trying to redirect
from the below page:
https://example.org/content/content.php?PrintMe=0&PgName=gothedistance
to this page:
https://example.org/gothedistance
My url redirect rule looks like the below:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect" stopProcessing="true">
<match url="content.php$" />
<conditions>
<add input="{QUERY_STRING}" pattern="PrintMe=0&PgName=gothedistance" />
</conditions>
<action type="Redirect" url="gothedistance" redirectType="Permanent" appendQueryString="false" />
</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="

How do I redirect a specific port in the IIS server to an other port

My URL Rewrite rule only works for port 80 in IIS .
The rewrite works for : http://localhost:80 --> http://localhost:8100
The rewrite works for : http://localhost:80/redirect --> http://localhost:8100
The rewrite doesnt work for : http://localhost:8080 --> http://localhost:8100
The rewrite doesnt work for : http://localhost:8080/redirect --> http://localhost:8100
My web.config is following:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="true" showFlags="Date, Time, Size, Extension, LongDate" />
<rewrite>
<rules>
<rule name="Redirect to port 8100" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{SERVER_PORT}" pattern="^8100$" negate="true" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}:8100/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The web.config is only working on default http port 80/443 but not for an specific port.
Which changes has to be made in the web.config for working with all ports?
The main problem is the variable {HTTP_HOST} which already contains the PORT information.
To solve this issue you need to adjust two things:
Extend the matching pattern within
Set the correct address for the redirect
I adjusted your web.config to see how it should work with your example:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="true" showFlags="Date, Time, Size, Extension, LongDate" />
<rewrite>
<rules>
<rule name="Redirect to port 8100" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" >
<add input="{HTTP_HOST}" pattern="([^/:]*?):[^/]*?" />
</conditions>
<action type="Redirect" url="http://{C:1}:8100/{R:0}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

IIS got ERR_TOO_MANY_REDIRECTS on rewrite rule

I'm trying to create an IIS rule to redirect mobile users to the mobile site.
The main site (desktop version) is in the root route ('/'), and the mobile site is in the route '/mobile'
I created this IIS rule, but when I try it in desktop it's work well, but in mobile, I got an error ERR_TOO_MANY_REDIRECTS
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite Mobile" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="true"/>
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_USER_AGENT}" pattern="midp|mobile|phone" />
<add input="{HTTP_X-Device-User-Agent}" pattern="midp|mobile|phone" />
<add input="{HTTP_X-OperaMini-Phone-UA}" pattern="midp|mobile|phone" />
</conditions>
<action type="Redirect" url="mysiteurl/mobile" appendQueryString="false" />
</rule>
</rewrite>
</system.webServer>
</configuration>
It is happening because your rule is also matching all mobile URLs. You need to exclude mobile URLs from your rule. This rule will work for you:
<rule name="Rewrite Mobile" enabled="true" stopProcessing="true">
<match url="mobile(.*)" negate="true"/>
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_USER_AGENT}" pattern="midp|mobile|phone" />
<add input="{HTTP_X-Device-User-Agent}" pattern="midp|mobile|phone" />
<add input="{HTTP_X-OperaMini-Phone-UA}" pattern="midp|mobile|phone" />
</conditions>
<action type="Redirect" url="/mobile" appendQueryString="false" />
</rule>

HTTPS redirect in web.config leads to ERR_TOO_MANY_REDIRECTS

I'm trying to add a redirection rule in web.config to only serve my webpage in HTTPS and no HTTP at all; so far I've used this ruleset (together with my 404 redirect):
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Redirect to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
</rules>
</rewrite>
<directoryBrowse enabled="false" />
<httpErrors errorMode="Custom" existingResponse="Auto">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" subStatusCode="-1" prefixLanguageFilePath="" path="index.php" responseMode="Redirect" />
</httpErrors>
</system.webServer>
</configuration>
However, this leads to ERR_TOO_MANY_REDIRECTS; what is wrong with this web.config to cause this? It seems to work for other users.
I have already tried suggestions in these posts; Redirect loop when forcing HTTPS, URL Rewrite causing redirect loop, this blog and this blog.
Maybe I came here a bit late, but the following Rewrite rule helps to solve the redirection error:
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^example\.com$" />
<add input="{SERVER_PORT}" pattern="^80$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>

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>