Asp.net implements URL rewriting without extension. It is simple, convenient, and does not require isapis.

Source: Internet
Author: User

There are already many methods for URL rewriting, but most of them all have a disadvantage that web pages without an extension are not supported. For example, converting a http://www.aaa.com/jyk to a http://www.aaa.com/topic.aspx? Id = jyk.

The methods provided by Microsoft need to create a jyk folder in the root directory, and then create a default. aspx file. If you have 100 members, this is unimaginable.

Another method is to use the ISAPI filter, but this usually requires installing a DLL file on the server, and it is hard to do it without a server.

The following provides a method I have invented-I don't know if anyone has thought of it before-using two features for implementation.

First, the file cannot be found in IIS.

Second: Server. Transfer ("topic. aspx? Id = jyk ");

These two combinations can achieve our goal.

Idea: Use 404bCapture customer requests with errors before usingServer. TransferTurn.

When we access the http://www.aaa.com/jyk, assuming there is no jyk folder on the server, what results will be returned? Will return a page "this page cannot be found", can this return result be controlled by yourself? Fortunately, IIS provides this function. We can specify such errors to a page on the website.

Procedure:

Step 1: Open IIS manager, website properties -- custom error tag -- 404 error -- click Modify -- modify "Message Type" to "url ", enter the Page name for handling this error in the text box below, such as/none. aspx.


Step 2: Create a none. aspx webpage under the root directory of our website. Write some content (for testing) at will ).

After the settings are complete, visit http://www.aaa.com/jykto display the webpage content of none. aspx. Note that the URL in the browser is still http://www.aaa.com/jyk, but not http://www.aaa.com/none.aspx. This is very important. If the address changes, it will be meaningless.

So how can we transfer the page to the desired page? When all files cannot be found, we will go to the none. aspx file. Obviously this is not the final result we want. So how can we get the URL entered by the customer?

Open the background file of none. aspx,

String url = request. url. query;

 

We can use the preceding statement to obtain the URL address. The obtained URL format is:

? 404 http://www.aaa.com/jyk

Where "? 404 "is fixed, and it is the URL address in the browser.

The rest is to convert the web page as needed. You can split the string and use switch to judge it. You can also use a regular expression to judge it. Because I am not familiar with regular expressions, I use the switch method. Please refer to other documents for regular expressions. I am also looking for them. Please share them if you find them. Thank you.

Advantages:

1. webpage without extension is supported. You do not need to create folders or files.

2. The idea is simple, the usage is simple, the Code is not complicated, and no profound theory is involved;

3. Support for search engines, that is, they can be searched by various search engines.

4. It can be used with wildcard domain name resolution.

Disadvantages:

1. Modify the IIS 404b error handling method;

2. The efficiency should be slightly lower.

3. If you only use the. aspx extension, you do not need to modify IIS.

4. asp is not supported. Because ASP server. Transfer cannot contain parameters (? Id = jyk). It makes little sense to overwrite parameters that cannot be passed.

This method has been applied and the effect is very satisfactory. Http://www.1380000.com/and http://www.1370000.com/are using this method.

Related Article

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.