It is not difficult to implement pseudo-static URL rewriting during website Seo.

Source: Internet
Author: User

People on the earth know that most of the results of Website access statistics come from Baidu search and a few from Google.

One of the most important aspects of Seo is to rewrite the path, that is, rewrite the URL to implement pseudo-static. I have never done it before. I think it may be complicated. I tried it and it turned out to be good.

In general, there are three steps: Step 1: Add configuration in Web. config, Step 2: Write a URL rewriting class, inherit the ihttphandler interface, and Step 3: IIS configuration

Step 1: Add configuration in Web. config

Open vs2008, create a website urlrewrite, open the Web. config file, and add the configuration information and location in it.

Add a subnode to the httphandlers node shown in the figure. verb indicates the request type, such as get and post, path indicates the path, and type indicates the class to process the request. * Represents a wildcard. The following is an example

1. For example, rewrite to a directory.

 
<AddVerb= "*"Path= "Test /*"Type= "Customhttphandler"/>

Indicates URL redirection for all files under the test directory. The customhttphandler file is a custom rewrite class.

This directory can exist and does not exist,There is a difference between the two: If CSS, JS, images, and file paths exist, they can use relative paths. If they do not exist, CSS and JS will adopt absolute paths.

2. For example, to rewrite a file into a certain type, this suffix can be defined.

 
<AddVerb= "*"Path= "Product/*. htm"Type= "Customhttphandler"/>

It indicates that URL redirection is required for HTML files contained in the product directory in the Link. If the extension name you defined is not included in IIS extension, you need to add extension mappings in IIS.

For example, the address before rewriting may be product/default. aspx? Id = 2, then rewrite it to product/2.htm

Step 2: add the rewrite class customhttphandler. CS

The customhttphandler. CS file inherits the ihttphandler interface, which includes the isreusable attribute and the processrequest method,

Processrequest: Used to process HTTP requests.
Isreusable: Indicates whether multiple HTTP requests of the same type can be reused.

The following code is rewritten.CodeFor the above two examples

 Public   Class  Customhttphandler: ihttphandler {  Public  Customhttphandler (){  //          //  Todo: add the constructor logic here  //  }  // Reusable or not      Public   Bool  Isreusable {  Get  {  Return   True  ;}}  //  HTTP Request Processing      Public   Void  Processrequest (system. Web. httpcontext context ){  // Get current link address          String Url = Context. Request. url. pathandquery. tostring ();  String Realurl = String  . Empty;  //  Generic implementation is used here Dictionary < String , String > Urldic = New Dictionary < String , String >(); Urldic. Add (  "  /Test  " , "  /News/default. aspx  " ); //  Virtual directory example Urldic. Add ( "  Product/(\ w00000000.htm  " , "  Product/default. aspx? Id = $1  " );//  Pseudo-static example  //  Loop judgment          Foreach ( VaR Item In  Urldic ){  //  Match              If  (RegEx. ismatch (URL, item. Key, regexoptions. ignorecase )){  //  Replace Realurl =RegEx. Replace (URL, item. Key, item. Value, regexoptions. ignorecase );}}  If (Realurl! = ""  ){  //  Directly execute the real URL  Context. server. Execute (realurl );}  Else  {Context. response. Write (  "  Rewriting exception  "  ) ;}Context. response. End ();}} 

This class uses the most regular expressions, matching and replacement. It takes 1-2 hours to learn regular expressions. It is basically easy to use. You can add () to the place you want to replace. I have been doing this for a long time and cannot replace it all the time.

So far, the test can basically be run in vs2008. There is an example in the default. aspx file.

Step 3: Configure IIS

If you do not modify IIS after it is published and deployed to IIS, when you open the link, the 404 error message indicating that the file cannot be found is displayed,

Take iis5.1 as an example. Because I use the XP system, find the Home Directory tab and click Configure to bring up the application.ProgramConfiguration window,

Add an extension identical to. aspx, but the extension name is htm. Note the following:

Check whether the file exists. √ must be removed.

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.