Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL Rewrite IIS 7 - url redirect not working

I have a website running on IIS 7 bound to port 80 with 2 domains (for our purposes - example.com & test.com) pointed at it.

example.com is our canonical name so I would like any client that hits test.com to be redirected to example.com.

I have been trying to use the IIS 7 Rewrite module. However it doesn't seem to have any effect whatsoever. How can I trouble shoot this?

Here's the rule I put in my web.config.

<rewrite>
   <rules>
      <rule name="rule1" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
          <match url="*test.com*" />
          <action type="Redirect" url="{R:1}example.com{R:2}" />
      </rule>
   </rules>
</rewrite>
like image 380
dan Avatar asked Jan 11 '10 00:01

dan


People also ask

How do I enable redirect in IIS 7?

In the Web Server (IIS) pane, scroll to the Role Services section, and then click Add Role Services. On the Select Role Services page of the Add Role Services Wizard, expand Common Http Features, select HTTP Redirection, and then click Next. On the Confirm Installation Selections page, click Install.

How do I redirect a URL to another URL in IIS?

In IIS Manager, expand the local computer, right-click the Web site or directory you want to redirect, and click Properties. Click the Home Directory, Virtual Directory, or Directory tab. Under The content for this source should come from, click A redirection to a URL.

Why is my redirect not working?

A URL may not be redirecting for many reasons: Browser cache – Your browser has cached a previous request. Clear your browser cache to get the latest. Server cache – Your site is being cached with some caching software, and this needs to be updated to include your redirect.


1 Answers

I was going about it the wrong way. This is the way to do it:

<rule name="Canonical Host Name" stopProcessing="true">
  <match url="(.*)" />
  <conditions>
     <add input="{HTTP_HOST}" negate="true" pattern="^www\.example\.com$" />
  </conditions>
  <action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
</rule>

ref: http://blogs.iis.net/ruslany/archive/2009/04/08/10-url-rewriting-tips-and-tricks.aspx

like image 88
dan Avatar answered Sep 17 '22 22:09

dan