Removing .aspx from pages using rewriteModule?

后端 未结 3 1249
予麋鹿
予麋鹿 2020-11-27 18:12

I\'m using ASP .NET rewriteModule to rewrite http://example.com to http://www.example.com.

3条回答
  •  天涯浪人
    2020-11-27 18:39

    These are the standard rewrite rules I start every project with. I use only clean URLs for all the pages (example first rule works for www.example.com/about and second rule www.example.com/product/123)

    
    
      
        
        
      
      
        
        
       
    
    
    

    Pages where I need to parse out the ID (this case number only) and add it to the query string I add a similar rule to the front:

    
      
      
    
    

    If you want to use lower and upper case letters in the URL, set ignoreCase="true"

    Edit to answer your second question plus a bonus

    This rule will redirect aspx page to the clean URL:

    
      
      
    
    

    Replace url="{R:1}" with url="{ToLower:{R:1}}" to change URL to lowercase. See below why you would want to do this.

    Also a good idea to update the Form action so that post backs don't return back to the ugly URL. Using IIS 7.5 or newer this should work:

     if (!String.IsNullOrEmpty(Request.RawUrl))
            form1.Action = Request.RawUrl;
    

    or for IIS 7:

     if (!String.IsNullOrEmpty(Context.Request.ServerVariables["HTTP_X_ORIGINAL_URL"]))
            form1.Action = Context.Request.ServerVariables["HTTP_X_ORIGINAL_URL"];
    

    One more thing to keep in mind... it's a good idea to keep all URLs lower case. Mixing lower/upper case characters in the URL creates duplicate content issues for SEO/Google. For example website.com/About and website.com/about will load the same page, but Google will index them as two separate pages.

提交回复
热议问题