URL rewriting in Asp.net (no suffix rewriting)

Source: Internet
Author: User

On the Internet, you will often see that the address suffixes of many websites use xx. HTML or xx. aspx and other operations are similar to static file identifiers. have you ever wondered whether it is actually a static file generated one by one? What are the advantages and disadvantages of static file generation, for websites with a large volume of traffic, using static files can improve the stability and speed of the server. The disadvantage is that when a list is generated, a record is added and all related lists need to be reconstructed, of course, this issue cannot be ruled out by some companies,ProgramPersonnel, he is still a problem, and the generation of small and medium websites is not necessarily more dynamic than the number, because management is more troublesome.

Structure of the above facts, followed by pseudo-static technology, the standard is URL rewriting technology, he has the best support for the aspx format, of course, can also rewrite HTML files, however, this requires the setting of IIS, which is more convenient than the aspx format. This site uses the URL rewriting technology. This technology has obvious advantages: it is convenient for search engines to include and view, and so on. Then let's take a look at how to configure him.

First, we need to use the URL rewriting component:Urlrewriter.rar

Second, you need to make some settings in Web. config:

1. Add the following nodes in <system. Web> and </system. Web> of Web. config:

<Httpmodules>

  • <Add type = "urlrewriter. modulerewriter, urlrewriter" name = "modulerewriter"/>

      2. Add the following nodes in <configuration> and </configuration> of Web. config:

       

      <Configsections>

    1. <Section name = "rewriterconfig" type = "urlrewriter. config. rewriterconfigserializersectionhandler, urlrewriter"/> </configsections>
    2. <Rewriterconfig>
    3. <Rules> <rewriterrule>
    4. <Lookfor> ~ /Read (. [0-9] *) \. aspx </lookfor> <sendto> ~ /Read. aspx? Id = $1 </sendto>
    5. </Rewriterrule> <rewriterrule>
    6. <Lookfor> ~ /Read, (. [0-9] *), (. [0-9] *) \. aspx </lookfor> <sendto>
    7. <! [CDATA [~ /Read. aspx? Id = $1 & smallsortid = $2]> </sendto>
    8. </Rewriterrule> </rules>
    9. </Rewriterconfig>

        After setting the preceding two steps, you can basically run it. The <rewriterrule/> label contains a rewritten module, and the <lookfor/> label contains the rewritten address, <sendto/> is the original address. You should have noticed that I have written two groups of <rewriterrule/>. The first group is used for the single-parameter address, and the other is used for multi-parameter dynamic files.

         

        Note the following when using this method:

        1. You cannot use Windows to authenticate user permissions. Use Form for authentication. In web. config, configure it as: <Authentication mode = "forms"/>

        2. The path obtained using request. servervariables ["script_name"] is still: showplay. asp? Vid = 1

        3. If the address to be rewritten is sent back, the address to be overwritten will be showplay. asp? Vid = 1

        4. The suffix name must be. aspx. If it is another custom suffix name, for example,. net, map. Net to aspnet_isapi.dll in IIS so that. Net requests can

        To the Asp.net engine.

        Other tips:

        Many of you may have seen an address like this:

        Http://nmju.net/read/2007/07/11/135645/

        In fact, this is also rewritten, but the use of this must follow the establishment of the corresponding directory, such as the above address, you must create directories such as read, 2007, 07, 11, and 135645. Although there is no file in the directory, it is necessary to create one.

        (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

        (, 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

        (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

        (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

        (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 theoretically implemented. If there is an error,Leave a messageCorrect, thank you !)

        Rewrite Rules:

        <Add name = "rewrite1" virtualurl = "^ HTTP \: // (. *) .xianfen.net/default.aspx"

        Rewriteurlparameter = "excludefromclientquerystring"

        Destinationurl = "~ /Default. aspx? Id = $1"

        Ignorecase = "true"/>

      1. 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.