最近使用的主机从IIS6升级成了IIS8.5,博客也从WordPress转到了Typecho,伪静态的实现方式也有了变化,在IIS6里面是使用httpd.ini文件配置,而到了IIS8.5则是使用web.config文件配置。下面记录一下不同版本中IIS的伪静态配置。

IIS6主机

IIS 6主机的伪静态配置文件是httpd.ini文件(放在网站的根目录即可)。

WordPress

[ISAPI_Rewrite]   
# Defend your computer from some worm attacks   
#RewriteRule .*(?:global.asa|default\.ida|root\.exe|\.\.).* . [F,I,O]   
# 3600 = 1 hour   
CacheClockRate 3600   
RepeatLimit 32   
# Protect httpd.ini and httpd.parse.errors files   
# from accessing through HTTP   
# Rules to ensure that normal content gets through 
#RewriteRule /tag/(.*) /index\.php\?tag=$1  
RewriteRule /tag/[^/]+)/([^/]+)/?([0-9]+)?/ /index.php?tag=$1&paged=$3 [L]
RewriteRule /software-files/(.*) /software-files/$1 [L]   
RewriteRule /images/(.*) /images/$1 [L]   
RewriteRule /sitemap.xml /sitemap.xml [L]
RewriteRule /sitemap.html /sitemap.html [L]   
RewriteRule /favicon.ico /favicon.ico [L]
RewriteRule /xmlrpc.php  /xmlrpc.php  [L] 
# For file-based wordpress content (i.e. theme), admin, etc.   
RewriteRule /wp-(.*) /wp-$1 [L]   
# For normal wordpress content, via index.php   
RewriteRule ^/$ /index.php [L]   
RewriteRule /(.*) /index.php/$1 [L] 

# For category(中文分类以及分类翻页的规则)
RewriteRule /category/(.*)/page/(d+)$ /index.php?category_name=$1&paged=$2
RewriteRule /category/(.*) /index.php?category_name=$1

Typecho

[ISAPI_Rewrite] 
# 3600 = 1 hour 
CacheClockRate 3600 
RepeatLimit 32 
# 中文tag解决 
RewriteRule /tag/(.*) /index\.php\?tag=$1 
# sitemapxml 
#RewriteRule /sitemap.xml /sitemap.xml [L] 
RewriteRule ^sitemap.xml$ /index.php/sitemap.xml [L]
RewriteRule /favicon.ico /favicon.ico [L] 
# 内容页 
RewriteRule /(.*).html /index.php/$1.html [L] 
# 评论 
RewriteRule /(.*)/comment /index.php/$1/comment [L] 
# 分类页 
RewriteRule /category/(.*) /index.php/category/$1 [L] 
# 分页 
RewriteRule /page/(.*) /index.php/page/$1 [L] 
# 搜索页 
RewriteRule /search/(.*) /index.php/search/$1 [L] 
# feed 
RewriteRule /feed/(.*) /index.php/feed/$1 [L] 
# 日期归档 
RewriteRule /2(.*) /index.php/2$1 [L] 
# 上传图片等 
RewriteRule /action(.*) /index.php/action$1 [L] 

IIS8.5主机

IIS8.5主机的配置文件是web.config文件。

WordPress

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="category">
<match url="category/?(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="/index.php?category_name={R:1}" appendQueryString="false" logRewrittenUrl="false" />
</rule>
<rule name="tags">
<match url="tag/?(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="index.php?tag={R:1}" />
</rule>
<rule name="Main Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php/{R:0}" />
</rule>
<rule name="wordpress" patternSyntax="Wildcard">
<match url="*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule></rules>
</rewrite>
</system.webServer>
</configuration>

Typecho

文件1(cnurl.php):

<?php
if (isset($_SERVER['HTTP_X_ORIGINAL_URL'])) {
    // IIS Mod-Rewrite
    $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];
} else if (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
    // IIS Isapi_Rewrite
    $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL'];
} else {
    // Use ORIG_PATH_INFO if there is no PATH_INFO
    (!isset($_SERVER['PATH_INFO']) && isset($_SERVER['ORIG_PATH_INFO'])) && ($_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO']);
    // Some IIS + PHP configurations puts the script-name in the path-info (No need to append it twice)
    if (isset($_SERVER['PATH_INFO'])) {
        ($_SERVER['PATH_INFO'] == $_SERVER['SCRIPT_NAME']) ? ($_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO']) : ($_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO']);
    }
    // Append the query string if it exists and isn't null
    (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) && ($_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING']);
}
require("index.php");
?>

文件2(web.config):

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
          <rules>
            <rule name="cnUrl" stopProcessing="true">
              <match url="^(tag|category)/(.*)$" />
              <action type="Rewrite" url="cnurl.php" />
            </rule>
            <rule name="WordPress" patternSyntax="Wildcard">
              <match url="*" />
                <conditions>
                  <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                  <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                </conditions>
              <action type="Rewrite" url="index.php" />
            </rule>
          </rules>
        </rewrite>
    </system.webServer>
</configuration>

将上面两个文件都放到网站根目录。