?
Solved

Using RewriteMap in web.config for 301 redirects

Posted on 2013-06-21
5
Medium Priority
?
811 Views
Last Modified: 2014-03-17
Hi experts,

Tearing my hair out here.

I want to use the web.config file to handle some 301 redirects.  I have a RewriteMap and this is firing successfully but only if I don't include the domain name in the key value.  the problem is, I want to be able to have entries in the rewriteMap that have full domain and paths so that I can differentiate entries for different domains.  

e.g.

    <rewriteMaps>
       <rewriteMap name="Redirects">
           <add key="mydomain1.com/book-event/" value="myNEWdomain.com/events1/" />
           <add key="mydomain2.com/book-event/" value="myNEWdomain.com/events2/" />
       </rewriteMap>
    </rewriteMaps>

But as soon as I add the domain, the rule doesn't seem to fire/match and I just get a 404.

e.g. this works:

    <rewriteMaps>
       <rewriteMap name="Redirects">
           <add key="/book-event/" value="/events1/" />
       </rewriteMap>
    </rewriteMaps>

...but is of no use because I can't differentiate between domain names.

This is my code in full, from the web.config:

<rewrite>
    <rewriteMaps>
       <rewriteMap name="Redirects">
         <add key="mydomain1.com/book-event/" value="myNEWdomain.com/events1/" />
           <add key="mydomain2.com/book-event/" value="myNEWdomain.com/events2/" />
       </rewriteMap>
    </rewriteMaps>
      <rules>
      <rule name="Redirect rule1 for Redirects">
         <match url=".*" />
         <conditions>
       <add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
         </conditions>
         <action type="Redirect" url="{C:1}" appendQueryString="false" />
      </rule>

Any help, much appreciated.  Maybe I just have my regular expression all wrong?
0
Comment
Question by:jammy-d0dger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 27

Expert Comment

by:Sammy Ageil
ID: 39266184
You will need to use a rule for the {HTTP_HOST}
See the solution provided in this post
http://forums.iis.net/t/1155754.aspx
0
 

Accepted Solution

by:
jammy-d0dger earned 0 total points
ID: 39274473
Thanks, I did find this reference but the link you gave didn't give a working conclusion.  I went on to work out myself that I needed to combine the two. I also had to add http:// to the prefix if the domain was changing, otherwise it would redirect using a relative path.

<rewrite>
    <rewriteMaps>
       <rewriteMap name="Redirects">
         <add key="mydomain1.com/book-event/" value="http://myNEWdomain.com/events1/" />
           <add key="mydomain2.com/book-event/" value="http://myNEWdomain.com/events2/" />
       </rewriteMap>
    </rewriteMaps>
      <rules>
      <rule name="Redirect rule1 for Redirects">
         <match url="^.*" />
         <conditions>
       <add input="{Redirects:{HTTP_HOST}{REQUEST_URI}}" pattern="(.+)" />
         </conditions>
         <action type="Redirect" url="{C:1}" appendQueryString="false" />
      </rule>

This now works fully.  However, for the points, can you tell me how I would make this rule work if the visitor put www. prefix on the requested URL?  i.e. www.mydomain1.com/book-event/

Surely I don't have to duplicate the entire list of keys but with www. prefixing them all?

Help appreciated.
0
 
LVL 9

Expert Comment

by:tanujchandna
ID: 39276956
Hi,

The following is the example of redirection from http to https and its working fine for me. Try with following code by changing the urls required by you and it should work fine for you.

<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
0
 

Author Comment

by:jammy-d0dger
ID: 39277525
Sorry, not sure I follow. How does this become relevant to my question about making redirects work for both http://www.mydomain1.com AND http://mydomain1.com
0
 

Author Closing Comment

by:jammy-d0dger
ID: 39933705
No comments directly related to my question were offered.  Worked it out myself and posted code.
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question