. Net2.0 URL rewriting iirf

Source: Internet
Author: User
Tags servervariables
Introduction

IIRF is an open-source rewrite URL filter, similar to Apache URL rewriting. It is developed based on VC8.0 (you can use Visual Studio2005 or Visual C ++ 2005 Express to re-compile. It can run in IIS5.0 + and supports many formats such as ASP, ASP. NET, and PHP. Relative ratio of ASP. NET2.0 comes with URL rewriting, which has better performance and many features we need. The important point is that it supports URLs without extensions (for example, cnl bogs.com/###, you have no need to create a ult.aspxfile, iirfwill automatically help you resolve it ), what makes the URL easier for us to remember and further improves the search ranking? IIRF can capture the URL we requested in advance in aspnet_isapi for processing. If we access cnlbog s.com/a.aspx, then we obtain cnlbogs.com/a.htm, which needs to be merged, we need to know that this method cannot be implemented in ASP. NET.

IIRF is the same as ASP. net url rewriting. It is also based on the regular expression, with LOG records and request condition judgment. Go to the topic.

Install

We need to manually install IIRF. However. It is also very convenient.

1. Copy IsapiRewrite4.dll and IsapiRewrite4.ini to c: \ windows \ system32 \ inetsrv (you can also copy it to another appropriate folder ).

IsapiRewrite4.ini is the IIRF configuration file. After each change, IIRF automatically reloads the file. You do not need to restart IIS to reload the configuration. If the format of the modified INI file is incorrect, IIRF automatically obtains the correct configuration file.

2. Open IIS manager, select "default website", right-click "properties", select "ISAPI filter", click "add", and enter the Filter Name: Ionic Rewriter, select the IsapiRewrite4.dll file copied to c: \ windows \ system32 \ inetsrv and click "OK ".

3. Restart the IISADMIN service. (In Computer Management-windows Services)

4. Complete.

Logs

IIRF can load the INI configuration file, and your URL request records will be saved to the specified log file. Because it has a high performance overhead, we recommend that you set the log record level to 0, only
To facilitate debugging, you can set it to 5,

RewriteLog <filename stub> stores the Log Path, for example, c: \ temp \ iirfLog. out.
RewriteLogLevel {0, 1, 2, 3, 4, 5} log level. The default value is 0.

0-logs are not logged
1-A few logs
2-more logs
3-more detailed logs
4-detailed log (4), and will track server variable and the replaced string.
5-detailed logs (5), including log file change events. It is recommended that you use the log file during debugging.

Regular Expression

The regular expression syntax is the same as. NET, but the format is different. So I will not detail it either. For details about the regular expressions, you can use GOOGLE to search.

Format:
RewriteRule <url-pattern> <replacement-string> [<modifiers>]
Url-pattern: the matching Regular Expression (required)
Replacement-string: string to be replaced (required)
Modifiers: Operation tag for RewriteRule. Optional. Below I will explain

By default, the IIRF url-pattern and replacement-string regular are prefixed with the Host header.

For ease of description, let's take a look at several examples (the following examples are basically all from the IIRF documentation)

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

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

Target: 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

Target: http: // xxx/chiesa/pics. aspx? DNT 30&p;1.jpg

It is relatively simple, mainly because of the modifiers function. The following lists all its values and allow combinations (such as [R, L]).

R = Redirect (URL jump to the <replacement-string> address)
NF = Not found (Error 404 is returned to the user, but the file is Not removed and is still stored on the website)
L = Last test if match (if matched, the matching will not continue)
F = Forbidden (similar to the NF mark ,)
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, we changed the browser direction and jumped to the new specified URL.
[R = code] allows us to specify a specific HTTP status return code. It can only be between 301 and 399. If this parameter is exceeded. The status 302 is used by default.
RewriteRule ^/goto. aspx? R = (. *) $1 [R]
Source: http: // xxx/goto. aspx? R = http://www.google.com/
Goals: http://www.google.com

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

[NF]
It has been briefly introduced above. It can also work with RewriteCond to implement custom 404 error requests.
Note that the file to be matched must exist. The replaced string cannot be a file name.
RewriteRule ^/1008. aspx $/1. aspx [NF]
1008. The aspx file must exist, and 1. The aspx file does not exist. Otherwise, our results will fail.
(It's strange. I don't know if I made a mistake. However, this is the final test result, which is not described in detail in this document. If you have any idea, please tell me the reason)

[F]
Not described.

Fuzzy match


Save the original url to the HTTP_X_REWRITE_URL server variable.
In ASP. NET, you can use Request. ServerVariables ["HTTP_X_REWRITE_URL"] to obtain the original value.

RewriteCond
Rewritecond <Test-string> <pattern> [<modifier flag [,...]>]
Similar to condition judgment, and multiple conditions, or, and are allowed. Rewriterule is executed only when the server variable of rewritecond matches the specified regular expression. For example:
Rewritecond % {remote_addr} ^ (127.0.0.1) $
Rewriterule ^/(. *). aspx $/$1. aspx
If the IP address for accessing the website is 127.0.0.1, rewriterule ^/(. *). aspx $/$1. aspx is allowed.

Rewritecond % {remote_addr} ^ (127.0.0.1) $ [or]
Rewritecond % {remote_addr} ^ (192.168.0.10) $
Rewriterule ^/(. *). aspx $/$1. aspx
Added or to multiple condition 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 flags has two values.
I = fuzzy match
R = logical judgment

Iirf's rewritecond function is really flexible, and I don't know how to rewrite iis7. I haven't seen it yet * ^ _&. In addition. The [patterns] of rewritecond can contain 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 this path exists.
-F
Treats the TestString as a pathname and tests if it exists and
Is a regular file.
TestString is a path name and an existing file
-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 with a file exceeding 0 bytes.

Example used in this document
(1) RewriteCond % {HTTP_URL} (/| \. htm | \. php | \. html |/[^.] *) $)
(2) RewriteCond % {REQUEST_FILENAME }! -F
(3) RewriteCond % {REQUEST_FILENAME }! -D
(4) RewriteRule ^. * $/index. aspx [U, L]
(1) If the URL is htm, php, html (fuzzy match ),
(2) The URL is not a file.
(3) The URL is not the requested path.
(4) redirect all requests to index. aspx and save the original URL.

In Example
RewriteCond % {HTTP_USER_AGENT} ^ Mozilla .*
RewriteRule ^/$/homepage.max.html [L]

Other IIRF configuration attributes
IterationLimit {integer} matches the RewriteRule group after the specified integer. If the number of RewriteRule is exceeded, the default number starts from 8th.
Total number of MaxMatchCount {integer} RewriteRule groups.
RewriteLog <filename stub> Log Path
RewriteLogLevel {0, 1, 2, 3, 4, 5} Log Level

A common problem
Test ASP after installing IIRF. the URL under. NET. NET rewrite URL has the same problem: the path of the action under the Form cannot be rewritten, and we use Request. the original URL obtained by RawUrl is empty. Maybe you noticed the modifiers option of the RewriteRule I wrote above.. We can save the original URL to the server variable. You can obtain it through Request. ServerVariables [name. Then rewrite the value of action.

Code 1
1 Public Class FormFixerHtmlTextWriterClass FormFixerHtmlTextWriter
2 Inherits System. Web. UI. HtmlTextWriter
3 Private _ url As String
4 Public Sub New () 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 () Sub WriteAttribute (ByVal name As String, ByVal value As String, ByVal encode As Boolean)
9 If (Not _ url Is Nothing And String. Compare (name, "action", True) = 0) Then
10 value = _ url
11 End If
12 MyBase. WriteAttribute (name, value, encode)
13 End Sub
14 End Class
Postscript:
IIRF has analyzed the problem. It can be seen from the above that the function is indeed very powerful and the configuration is flexible. I found myself gradually fond of it. Hey. It is important. It is free and open-source.

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

: Http://file.mofile.com/
Source code extraction code: 7984919928322027
Extracted Code Compiled: 5251154719812491

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.