Skip to content

Instantly share code, notes, and snippets.

@greggnakamura
Last active November 2, 2015 19:05
Show Gist options
  • Save greggnakamura/ad10f8bb4de75803cff6 to your computer and use it in GitHub Desktop.
Save greggnakamura/ad10f8bb4de75803cff6 to your computer and use it in GitHub Desktop.
IIS: Rewrite Maps in URL Rewrite Module
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>URL Rewrite Module Test</title>
</head>
<body>
<h1>URL Rewrite Module Test Page</h1>
<table>
<tr>
<th>Server Variable</th>
<th>Value</th>
</tr>
<tr>
<td>Original URL: </td>
<td><%= Request.ServerVariables["HTTP_X_ORIGINAL_URL"] %></td>
</tr>
<tr>
<td>Final URL: </td>
<td><%= Request.ServerVariables["SCRIPT_NAME"] + "?" + Request.ServerVariables["QUERY_STRING"] %></td>
</tr>
</table>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rewriteMaps>
<rewriteMap name="StaticRewrites">
<add key="/default" value="/default.aspx?id=1&amp;title=some-title" />
<add key="/some-title" value="/default.aspx?id=1&amp;title=some-title" />
<add key="/post/some-title.html" value="/default.aspx?id=1&amp;title=some-title" />
</rewriteMap>
<rewriteMap name="StaticRedirects">
<add key="/old-default.aspx?id=1" value="/default.aspx?id=1" />
<add key="/posts/default.aspx?id=1" value="/default.aspx?id=1" />
<add key="/old-title.html" value="/default.aspx?id=1" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Rewrite Rule">
<match url=".*" />
<conditions>
<add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" />
</rule>
<rule name="Redirect Rule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{StaticRedirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="http://localhost{C:1}" appendQueryString="False" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment