There are many benefits of URL rewriting, such as SEO, memory-friendly, hiding real paths to improve security and update convenience. This article describes the implementation of various URL rewriting methods.
ASP. there are many methods to implement URL rewriting in NET 2.0, such as Global. use Application_BeginRequest to capture requests in asax, and use the Rewrite method of the HttpContext class or Server. the Transfer method is used for rewriting; The IHttpModule is implemented for rewriting; the server 404 error is also used to direct to a new page for rewriting; finally, rewrite with components (the basic principle is to implement the IHttpModule and IHttpHandler interfaces to process requests ).
If you process the request yourself, it is difficult to implement it, especially in large projects, and you must use components for implementation. There are many open-source or free URL rewriting components on the Internet, so there is a lot of room for choice. At the beginning, I chose a small, fast, and frequently-used URL rewriting function. After comparison, select only 32 KB UrlRewritingNet.
The main functions of this document are as follows:
1. Regular Expression-based Rewriting
2. Support for Themes and master pages
3. Support for output caching
4. Support environments with the "Medium" trust level
5. Postback is supported.
6. Supports Cookie-free sessions.
7. Add and modify rewrite rules at runtime
8. easy to install and use
OK! If the function is sufficient, select him!
Configuration in Web. config:
<Configuration>
<ConfigSections>
<Section name = "urlrewritingnet"
RequirePermission = "false"
Type = "UrlRewritingNet. Configuration. UrlRewriteSection, UrlRewritingNet. UrlRewriter"/>
</ConfigSections>
<Urlrewritingnet
RewriteOnlyVirtualUrls = "true"
ContextItemsPrefix = "QueryString"
DefaultPage = "Default. aspx"
DefaultProvider = "RegEx"
Xmlns = "http://www.urlrewriting.net/schemas/config/2006/07">
<Rewrites>
<Add name = "RewritePhoto" virtualUrl = "^ ~ /(\ D +). aspx"
RewriteUrlParameter = "ExcludeFromClientQueryString"
DestinationUrl = "~ /Default. aspx? ID = $1"
IgnoreCase = "true"/>
<! -- More Rewrite Rules -->
</Rewrites>
</Urlrewritingnet>
<AppSettings/>
<ConnectionStrings/>
<System. web>
<HttpModules>
<Add name = "UrlRewriteModule"
Type = "UrlRewritingNet. Web. UrlRewriteModule, UrlRewritingNet. UrlRewriter"/>
</HttpModules>
<! --
Set compilation debug = "true" to insert the debugging symbol
Compiled pages. However, this
Performance is affected, so this value is only available during development.
Set to true.
-->
<Compilation debug = "true"/>
<! --
In the <authentication> section, you can configure
Security Authentication mode,
To identify the user.
-->
<Authentication mode = "Windows"/>
<! --
If an unprocessed error occurs during request execution,
You can configure the corresponding processing steps in the <mermerrors> section. Specifically,
This section allows developers to configure
Html error page to be displayed
To replace the error stack trace.
<CustomErrors mode = "RemoteOnly" defaultRedirect = "GenericErrorPage.htm">
<Error statusCode = "403" redirect = "NoAccess.htm"/>
<Error statusCode = "404" redirect = "FileNotFound.htm"/>
</CustomErrors>
-->
</System. web>
</Configuration>
File used for testing: Default. aspx
<% @ 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 runat = "server">
<Title> URL rewriting test floating remote http://xianfen.net </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<%
Response. Write ("original URL:" + Request. RawUrl + "<br/> ");
Response. Write ("obtained ID:" + Request. QueryString ["ID"]);
%>
</Div>
</Form>
</Body>
</Html>
(1) rewrite without changing the extension:
Rewrite Rules:
<Add name = "RewritePhoto" virtualUrl = "^ ~ /(\ D +). aspx"
RewriteUrlParameter = "ExcludeFromClientQueryString"
DestinationUrl = "~ /Default. aspx? ID = $1"
IgnoreCase = "true"/>
IIS configuration: (this configuration should be the default configuration, but some virtual host providers have modified this configuration)
Website-> properties-> directory-> Configuration (G)...-> Ing-> application extension-> extension. aspx-> edit-> "Confirm file existence" check box not selected
Running result:
(, Pseudo-static rewriting, extended name: .html, etc.
Rewrite Rules: <add name = "RewritePhoto" virtualUrl = "^ ~ /(\ D1_0000.html"
RewriteUrlParameter = "ExcludeFromClientQueryString"
DestinationUrl = "~ /Default. aspx? ID = $1"
IgnoreCase = "true"/>
IIS configuration: website> Properties> directory> Configuration (G)...-> Ing-> application extension-> Add
Executable File: c: \ windows \ microsoft.net \ framework \ v2.0.50727 \ aspnet_isapi.dll
Extension:. html
Action: limit to GET, HEAD, POST, and DEBUG
Script Engine: Selected
Check whether the file exists: No
Running result:
(3) rewrite any extension, for example, the extension is. zxjay.
Rewrite Rules:
<Add name = "Rewrite1" virtualUrl = "^ ~ /(\ D +). zxjay"
RewriteUrlParameter = "ExcludeFromClientQueryString"
DestinationUrl = "~ /Default. aspx? ID = $1"
IgnoreCase = "true"/>
IIS configuration: Same as above
Running result:
(4) rewrite without a suffix
Rewrite Rules:
<Add name = "Rewrite1" virtualUrl = "^ ~ /(\ D +)/Default. aspx"
RewriteUrlParameter = "ExcludeFromClientQueryString"
DestinationUrl = "~ /Default. aspx? ID = $1"
IgnoreCase = "true"/>
IIS configuration: website> Properties> directory> Configuration (G)...-> Ing-> wildcard application ing-> insert
Executable File: c: \ windows \ microsoft.net \ framework \ v2.0.50727 \ aspnet_isapi.dll
Check whether the file exists: No
The principle is: When you request "Current directory", find the default document and rewrite it. Therefore, rewrite "^ ~ /(\ D +)/Default. aspx "must correspond to the first Default document.
For more information about "wildcard application ing", refer to Microsoft's description.
Running result:
(5) rewrite of second-level domain names to multi-level domain names
(Note: Due to restrictions on conditions, this rule is not tested and can be implemented theoretically. If there is an error, please leave a message to correct it. Thank you !)
Rewrite Rules:
<Add name = "Rewrite1" virtualUrl = "^ http \: // (. *) .xianfen.net/Default.aspx"
RewriteUrlParameter = "ExcludeFromClientQueryString"
DestinationUrl = "~ /Default. aspx? ID = $1"
IgnoreCase = "true"/>
Note:
You can define Rewrite Rules and the number of parameters. You can also add or modify rewrite rules at runtime. You can also extend the functions of this component to meet various requirements. For more information, see the documentation and source code.
The example in this article is only tested on the local machine, and there is no strict test, only the first rewrite method is used on the virtual machine, is the rewrite rules that you see for this personal blog system (http://xianfen.net.
If any error or omission is found, please leave a message and we are very grateful. We hereby declare that we should avoid the suspicion of mistaken children.