################################################################################ # # rules.ce - A Sample CustomError Redirection Rules File # ################################################################################ # ######################## (pound signs denote comments) ######################### # # # Introduction # # # CustomError's Redirection Rules allow you to single-out specific 404 errors by # their original request URLs, and then redirect those requests to any URL of # your choosing. # # # WARNING: Everything written in this sample rules file is meant purely # to provide instructions on how to use a CustomError Redirection Rules file. # The '#' symbol keeps CustomError from trying to operate on the text that # follows it in a line. Once you are comfortable with CustomError redirection # rules, you can delete everything in this file, except any complete sample # rules you would like to modify and keep for your own use. # # # Redirection URLs can point to a target page designed especially to handle a # certain sub-set of 404s, or they can redirect the user to a different section # of the Web site, or to a different Web site altogether. # # You can also pass useful information about the original request (such as the # original URL, the query string or the referring URL) to the target page, or # even use this information to build the redirection URL itself. # # And since the Redirection Rules live in a rules.ce file (such as this one), # which in turn lives in the Web site's root directory, they can be set up and # modified by anyone with write access to that file. Administrative access to # the Web server itself is not required. # # This "delegated administration" allows anyone with the appropriate level of # file access permissions (whether a developer, a site manager, or a # SEO specialist) to centrally administer the 404 redirection policies for an # entire site. # # ################################################################################ # # # Understanding Redirection Rules # # # Each Redirection Rule is on its own line, and consists of at least two parts, # separated by a tab. An optional third part is also separated by a tab. The # basic syntax of a Redirection Rule can be represented like this: # # Request-Selector TAB Redirection-URL [ TAB Redirection-Type ] # # In this representation, TAB means what you get when you hit the tab key, and # the square brackets mean that everything inside them is optional. The other # three identifiers represent the three main parts of the rule, as described # below. # # ################################################################################ # # # 1. Request-Selector # # This stands for the pattern that will be used to select a particular 404, or # set of 404s, for redirection. When a 404 error is raised by IIS, this pattern # will be applied to the original request URL and, if a match is found, the 404 # error response will be replaced with a redirection message, telling the # browser to request a different URL. # # The Request-Selector is based on the path portion of a request URL that # generates a 404 error. It can pick out a specific URL, or it can include an # asterisk (*) wildcard that allows it to match several URLs. # # For example, the Request-Selector # # /products/mypage.htm # # would only match a request for the URL http://hostname/products/mypage.htm. # But the Request-Selector # # */mypage.htm # # would match any of the following requests: # # /mypage.htm # /products/mypage.htm # /foo/bar/mypage.htm # # # Order of Evaluation # # Note that the Redirection Rules in a rules file will be evaluated top to # bottom, and that the first rule with a matching Request-Selector is the one # that will be executed. Thus, if you want to intermix more-specific and # less-specific Request-Selectors, you need to place the more-specific ones on # top, like so: # # /products/mypage.htm # */mypage.htm # # If these two rules are both in the rules file, and in this order, then the URL # # /products/mypage.htm # # will match the first rule, but the URL # # /products/us/mypage.htm # # will match the second rule. # # ################################################################################ # # # 2. Redirection-URL # # This stands for the URL to which the user will be redirected. Technically, it # is the URL that CustomError will insert into the "Location" header of the # redirection response. # # Redirection-URL can be fully qualified # (e.g., http://hostname/products/index.htm) or a relative URL # containing only the path portion (e.g., /products/index.htm). Fully qualified # URLs can reference other Web sites, while relative URLs always reference the # current site. # # Note: Always use a fully-qualified URL when redirecting to a secure # (HTTPS/SSL/TLS) connection. # # Here, for example, are several versions of a single Redirection Rule, each # with a different Redirection-URL. Depending on which one of these rules was # present in the rules file, the user who requested the missing file # /products/mypage.htm would be redirected to the same site, or to a different # site (over HTTP or HTTPS, or even to a completely different site and URL): # # /products/mypage.htm /products/mypage.aspx # /products/mypage.htm https://hostname/products/mypage.aspx # /products/mypage.htm http://hostname2/products/mypage.aspx # /products/mypage.htm https://hostname2/products/mypage.aspx # /products/mypage.htm http://hostname3/about/newpage.aspx # # Note: Be careful, when creating your redirection URLs, not to set up any # infinite loops! # # # Special Variables # # A Redirection-URL can contain one or more special variables. These special # variables let you add information about the original request to the # Redirection-URL, either to construct the URL itself, or to pass the # information along in the query string. When CustomError finds one of these # special variables in the Redirection-URL, it replaces it with the # corresponding information. # # There are four special variables you can use in the Redirection-URL: # # $U - The full request URL, including the query string. # $P - The path portion of the request URL. # $Q - The query string portion of the request URL. # $R - The contents of the HTTP Referer header from the original request. # # # For example, if the request URL that generated the 404 was this: # # /products/mypage2.asp?var1=value1 # # # and if it was referred by this page: # # /products/mypage1.asp # # # and if the Redirection Rule was this: # # /products/* /ErrHandler.asp?path=$P&query=$Q # # # then the URL to which the user would be redirected would look like this: # # /ErrHandler.asp?path=%2Fproducts%2Fmypage2%2Easp&query=var1%3Dvalue1 # # # A redirected URL with this additional information appended would allow you to # do your own special handling of the 404 error in the script ErrHandler.asp, # for instance by reporting the original request information back to the user, # or by logging this information in a database, etc. # # In addition to passing information about the original request on the query # string of the redirect URL, You can also use the special variables to build # that URL. This can come in handy, for instance, if you want to set up a # pattern of redirects from an old domain to a new one, while preserving the # directory structure. To do that, you could use the following type of rule: # # /* http://newdomain$U # # NOTE: A simple rule like this one can handle all of the individual page # redirections for an obsolete domain and may help in the translation of search # engine relevancy/ranking from one domain to another. # # ################################################################################ # # # 3. Redirection-Type # # This represents the third and only optional part of a Redirection Rule. The # Redirection-Type determines the type of HTTP redirect used in the error # response. The possible values are: # # # "302" or "temp" for a temporary redirect ("302 Found"). # "301" or "perm" for a permanent redirect ("301 Moved Permanently"). # # # Here is a simple example that makes the redirect permanent: # # /products/mypage.htm /products/mypage.aspx perm # # # If the Redirection-Type is not included in the rule, the default is a 302 # redirect. # ################################################################################