MVC overrides in IIS6

Source: Internet
Author: User
Tags goto modifier modifiers servervariables stub

Every time watching Scottgur blog always harvest unexpected surprise ^_^. In his blog (Tip/trick:url rewriting with asp.net (thanks to Shi), he describes various ways to rewrite URLs in asp.net. My article does not cover how to rewrite URLs in ASP.net, but simply introduce IIRF (for the convenience of writing fewer words, the later Ionic ' Isapi Rewrite filter will all be abbreviated) How to implement a URL rewrite under IIS.

introduce

IIRF is an open-source rewrite URL filter, similar to Apache URL rewrite, based on VC8.0 (which can be recompiled with visual Studio2005 or Visual C + + Express). It can run in iis5.0+, support asp,asp.net,php and many other formats. Rather than asp.net2.0 's own URL rewrite, with better performance and many of the features we need, it is important that it supports URLs with no extensions (for example: cnlbogs.com/****, You do not need to create a default default.aspx file, IIRF automatically will help you resolve, so that the URL more convenient for our memory, but also to further improve the ranking of the search. IIRF can be aspnet_isapi in advance to capture the URL we requested to process, if we visit cnlbogs.com/a.aspx, need to get cnlbogs.com/a.htm, step needs (IIS-ASPNET_ISAPI), Through IIRF, we can jump directly aspnet_isapi.dll, direct access to a.htm, you know, this way in asp.net is not possible.

IIRF with the ASP.net rewrite URL, it is also based on a regular way to match, with Log records, the request of the conditional judgment. Let's get to the point.

installation

IIRF installation requires our manual operation to complete. But. is also very convenient.

1 will IsapiRewrite4.dll, Isapirewrite4.ini copy to C:\WINDOWS\system32\inetsrv (you can also copy to other appropriate folders below).

Isapirewrite4.ini is a IIRF configuration file, each time the file changes, IIRF will automatically reload the file, do not need to restart IIS to reload the configuration, if you modify the following INI file format is not correct, IIRF will automatically get the last correctly loaded configuration file.

2 Open IIS Manager, select Default Web site, right-click Properties, select ISAPI Filter, click Add, enter Filter Name: Ionic rewriter, copy the executable file to c:\windows\system32\ Inetsrv The following IsapiRewrite4.dll file, point "OK".

3 Restart IISAdmin service. (In Computer Management----Windows services)

4 complete.

Log

IIRF can load the INI configuration file, and the user's URL request record is saved to the specified log file. Because it has a significant performance overhead, it is recommended that the record level of its log be set to 0, with only
For ease of debugging, it can be set to 5,

Rewritelog <filename stub> saved log path, such as C:\temp\iirfLog.out
rewriteloglevel {0,1,2,3,4,5} log level with default value of 0

0– log is not logged
1-A little log
2-More logs
3-A more detailed log
4-Verbose log (4), and will track server variable and replacement strings.
5-Verbose log (5), including log file change events, recommended for easy debugging use

Regular

Regular grammar with. NET, just a different format. So I'm not in detail. Specific about the regular description you can use Google search.

Format:
Rewriterule <url-pattern> <replacement-string> [<modifiers>]
Url-pattern: matching regular expression (required)
Replacement-string: String to replace (required)
Modifiers: The action tag for the Rewriterule. Optional options. In the following I will explain

The default IIRF url-pattern,replacement-string is already in front of the main header.

For the sake of description, look directly at a few examples (the following example basically all source IIRF documents)

Rewriterule ^/original/(. *). php/modified/$1.aspx

Source: http://xxx/original/index.php

Goal: Http://xxx/modified/index.aspx

Rewriterule ^/dinoch/album/([^/]+)/([^/]+). (Jpg| Jpg| PNG)/chiesa/pics.aspx?d=$1&p=$2.$3

Source: http://xxx/dinoch/album/30/1.jpg

Goal: Http://xxx/chiesa/pics.aspx?d=30&p=1.jpg

Relatively simple, mainly lies in the function of modifiers. All of its values are listed below, allowing for a combination (such as [r,l]).

R= Redirect (URL jump to <replacement-string> address)
NF= Not found (returns a 404 error to the user, but the file is not removed or remains in the Web site)
L= Last Test if match (if it has already been matched, will not continue to match)
F= Forbidden (similar to NF logo,)
I= Do case-insensitive matching
U= Store original URL in server Variable http_x_rewrite_url (save the original URL to the HTTP_X_REWRITE_URL server variable.) )

[R] or [R=code]
Just like the redirect method we used in asp.net, change the direction of the browser and jump to the new specified URL.
[R=code] allows us to specify a specific HTTP status return code. Only between 301 and 399. If this is beyond this range. The default would be to use the 302 state.
Rewriterule ^/goto.aspx?r= (. *) $ $ [R]
Source: http://xxx/goto.aspx?r=http://www.google.com/
Goal: http://www.google.com

[L]
It has been briefly described above. Not description

[NF]
It has been briefly described above. It can also work with Rewritecond to implement a custom 404 error request.
Especially note that the file you want to match must exist, the replacement string does not allow the file name to exist
Rewriterule ^/1008.aspx$/1.aspx [NF]
1008.aspx files need to exist, 1.aspx does not exist, otherwise it will not be normal to achieve our results.
(Strangely, I don't know if I was mistaken.) But the results of my final test are indeed the case, the documentation is not detailed, there are friends who know can tell me why.

[F]
Not a description.

[I]
Fuzzy matching

[U]
Save the original URL into the HTTP_X_REWRITE_URL server variable.
In asp.net you can use request.servervariables["Http_x_rewrite_url" to get the original value.

Rewritecond
Rewritecond <test-string> <pattern> [<modifier flag[,...] "]
Similar to conditional judgment, and allows multiple conditions, Or,and. Rewriterule executes only if the Rewritecond server Variable matches the specified regular expression. Like what:
Rewritecond%{remote_addr} ^ (127.0.0.1) $
Rewriterule ^/(. *). aspx$/$1.aspx
If we visit the site's IP address from 127.0.0.1, then allow Rewriterule ^/(. *). aspx$/$1.aspx

Rewritecond%{remote_addr} ^ (127.0.0.1) $ [OR]
Rewritecond%{remote_addr} ^ (192.168.0.10) $
Rewriterule ^/(. *). aspx$/$1.aspx
Added or to multiple conditional judgments

Rewritecond%{remote_addr} ^ (?! 127.0.0.1) ([0-9]{1,3}.[ 0-9]{1,3}. [0-9] {1,3}. [0-9] {1,3}) (.*)$
Rewriterule ^/(?!) redirected.htm) (. *) $/redirected.htm

Modifier Flagshas two values
I= Fuzzy Matching
OR= Logical Judgment

From the IIRF of the Rewritecond function, is really very flexible, do not know how to rewrite with IIS7, Xi hee. Haven't seen it yet, *^_&. Other than that. Rewritecond [Patterns] can take a few of the following parameters
- D
Treats the teststring as a pathname and tests if it exists,
and is a directory.
TestString is a path name, and there is this path
- F
Treats the teststring as a pathname and tests if it exists and
is a regular file.
TestString is a path name and is a file that exists
- S
Treats the teststring as a pathname and tests if it exists and
is a regular file with size greater than zero.
TestString is a path name and exists with a file exceeding 0 bytes

such as the example used in the document
(1) Rewritecond%{http_url} (/|\.htm|\.php|\.html|/[^.) *) $ [I])
(2) Rewritecond%{request_filename}!-f
(3) Rewritecond%{request_filename}!-d
(4) Rewriterule ^.*$/index.aspx [u,l]
(1) If the URL is in htm,php,html (Fuzzy match),
(2) URL does not exist file
(3) URL is not the requested path
(4) Jump all requests to index.aspx, save the original URL, and then do not match this

In such
Rewritecond%{http_user_agent} ^mozilla.*
Rewriterule ^/$/homepage.max.html [L]

IIRF Other Configuration Properties
Iterationlimit {integer} matches the Rewriterule group from the specified integer. If the number of rewriterule is exceeded, the default will start at the 8th.
The total number of Maxmatchcount {integer} rewriterule groups.
Rewritelog <filename stub> Log path
Level of Rewriteloglevel {0,1,2,3,4,5} log

A common problem
After you have just installed IIRF, test the URL below asp.net to find the same problem as the previous ASP.net rewrite URL: Unable to overwrite the path of the action under form, and we use Request.rawurl to get the original URL is empty. Maybe you're starting to notice the rewriterule modifiers option [U] that I've written above. We can save the original URL to a server variable. And then through the request.servervariables[name] to get. Then rewrite the value of the action.


1 Public Class Formfixerhtmltextwriter
2 Inherits System.Web.UI.HtmlTextWriter
3 Private _url as String
4 Public Sub New (ByVal writer as TextWriter)
5 MyBase.New (writer)
6 _url = ForumContext.Current.Context.Request.ServerVariables ("Http_x_rewrite_url")
7 End Sub
8 public Overloads Overrides Sub WriteAttribute (ByVal name As String, ByVal value As String, ByVal encode as Boolea N
9 If (not _url are nothing and String.Compare (name, action, True) = 0) Then
Ten value = _url
One end If
Mybase.writeattribute (name, value, encode)
End Sub
End Class

Postscript:
IIRF has analyzed the same. As you can see from the above, the functionality is really powerful and the configuration is flexible. I found myself slowly liking it. Hey. The important thing is. It's free, open source.

Give yourself a little clap. Hehe It's not easy to write this essay. I hope I can give you a helping hand. In addition, because of their limited level, there may be a description of the wrong place or unclear place also please point out, Chance, Wuzegamin:)

Finally, I wish you all a happy first week of 2007 * ^_^ *

[2007-09-01]
http://zhangsichu.com/blogview.asp?Content_Id=82
There is already someone to achieve the two domain name rewrite, we can go to see. Thank Tombom for the information provided.

IIRF Website: http://cheeso.members.winisp.net/IIRF.aspx

Reproduced from: http://www.cnblogs.com/cnzc/archive/2007/03/02/662217.html

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.