Iis url rewriting
Posted on yiki reading (616) Comments (0) edit the category of the favorites: reprint information, ASP. NET
Iirf (ionic's ISAPI rewrite filt ER) entry, rewrite the URL on IIS
Every time I read a scottgur blog, I always get an unexpected surprise ^_^. In his blog (tip/TRICK: URL rewriting with ASP. NET (Thanks for thinking), he described various methods for rewriting URLs in ASP. NET. However, this article does not cover how to use ASP. net rewrite URL, just a brief introduction to iirf (to facilitate less writing a few words, the future ionic's ISAPI rewrite filter will be all abbreviated) How to rewrite the URL under IIS.
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, cnlbogs.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 cnlbogs.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.
Like rewriting a URL, iirf is also based on regular expressions, with log records and request conditions. 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 Service)
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
The iirfURL-pattern, replacement-stringThe Host header is included in front of the regular expression.
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 | 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 (the Error 404 is returned to the user, but the file is not removed and is still retained 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.
[I]
Fuzzy match
[U]
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 flagsThere are two values
I= Fuzzy match
Or= 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 |/[^.] *) $ [I])
(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
Source: http://www.devgg.com /? P = 119