Rewriting & redirecting : Example: Rewriting URLs using variables
 
Example: Rewriting URLs using variables
Example.com has a web site that uses ASP, but the administrator wants it to appear that the web site uses PHP. To do this, the administrator configured a rule that changes any requested file's extension which is asp into php.
The condition table contains two match conditions, in this order:
1. The Host: may be anything.
2. The request URL must end in .asp.
If both of those are true, the request is rewritten.
The administrator does not want to rewrite matching requests into a single URL. Instead, the administrator wants each rewritten URL to re-use parts of the original request.
To assemble the rewritten URL by re-using the original request’s file path and Host:, the administrator uses two back reference variables: $0 and $1. Each variable refers to a part of the original request. The parts are determined by which capture group was matched in the Regular Expression field of each condition table object.
$0 — The text that matched the first capture group (.*). In this case, because the object is the Host: field, the matching text is the host name, www.example.com.
$1 — The text that matched the second capture group, which is also (.*). In this case, because the object is the request URL, the matching text is the file path, news/local.
Table 46: Example URL rewrites using regular expressions
Example request
URL Rewriting Condition Table
Replacement URL
Result
www.example.com
HTTP Host
(.*)
$0
www.example.com
/news/local.asp
HTTP URL
/(.*)\.asp
/$1.php
/news/local.php
See also
Rewriting & redirecting
Example: Rewriting URLs using regular expressions
Example: HTTP-to-HTTPS redirect
Regular expression syntax
What are back-references?
Cookbook regular expressions