URL rewriting instance

Source: Internet
Author: User

The following uses a simple example for application:

First, put the downloaded intelligencia. urlrewriter. dll file in the program directory Bin

Add intelligencia. urlrewriter. dll file reference to the vs.net project.

2. Create an index. aspx File

The content of index. aspx. CS is as follows:

Protected void page_load (Object sender, eventargs E)

{

Response. Write (request ["PID"]);

}

That is to say, the PID content of the output parameter

3. Configure the Web. config file

The content is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>

<Configuration>

<! -- Set the address rewriting component -->

<Configsections>

<Section name = "rewriter" requirepermission = "false" type = "intelligencia. urlrewriter. configuration. rewriterconfigurationsectionhandler, intelligencia. urlrewriter"/>

</Configsections>

<! -- Address rewriting rule file -->

<Rewriter file = "~ /App_data/Rewrite. xml "/>

<Appsettings/>

<Connectionstrings/>

<System. Web>

<Compilation DEBUG = "false">

</Compilation>

<Authentication mode = "Windows"/>

<! -- Httpmodules -->

<Httpmodules>

<Add type = "intelligencia. urlrewriter. rewriterhttpmodule, intelligencia. urlrewriter" name = "urlrewriter"/>

</Httpmodules>

</System. Web>

</Configuration>

4. Start instance application

First effect experiment:

Normal access: http: // localhost: 4412/index. aspx? PID = Hao

Rewrite the URL to http: // localhost: 4412/index_hao.aspx.

The rewrite. XML content of the rewrite rule file is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>

<Rewriter>

<! -- ([A-Za-Z] +) Only English is allowed -->

<Rewrite url = "~ /Index _ ([A-Za-Z] +). aspx $ "to = "~ /Index. aspx? PID = $1 "Processing =" stop "/>

</Rewriter>

Original access:

Rewrite:

 

Second effect experiment:

Normal access: http: // localhost: 4412/index. aspx? PID = Hao

Rewrite the URL to http: // localhost: 4412/index/any character. aspx

The rewrite. XML content of the rewrite rule file is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>

<Rewriter>

<! -- ([A-Za-Z] +) Only English is allowed -->

<Rewrite url = "~ /Index _ ([A-Za-Z] +). aspx $ "to = "~ /Index. aspx? PID = $1 "Processing =" stop "/>

<! -- (. *) Any character -->

<Rewrite url = "~ /Index/(. *)/. aspx $ "to = "~ /Index. aspx? PID = $1 "/>

</Rewriter>

As follows:

 

The experiment is successful, isn't it easy !! You are welcome to exchange more and learn more original works from it.

Note: Use & amp; To pass multiple parameters;

For example, <rewrite url = "~ /Index/(. *)/(. *)/. aspx $ "to = "~ /Index. aspx? PID = $1 & amp; Act = $2 "/>

 

Supplementary instructions on converting .aspxto .html:

In fact, it is very simple. You only need to perform one step. The following uses ASP. net2.0 as an example to set it:

 

The executable file path is C:/Windows/Microsoft. NET/framework/v2.0.50727/aspnet_isapi.dll.

 

In this way, we can change the rule .aspxto .html for static access.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.