传递非现有目录作为IIS或Apache中的参数

时间:2009-08-22 09:42:49

标签: apache iis mod-rewrite url-rewriting isapi-rewrite

首先看一下这个网址:

https://stackoverflow.com/questions/tagged/xoxoxo/

此目录不存在,但不知何故stackoverflow可以将最后一个目录作为参数传递给他的基本脚本。

这可以配置IIS或Apache吗?怎么样?

1 个答案:

答案 0 :(得分:5)

这种行为背后的机制称为 url-rewriting ,可以在 Apache 中使用mod_rewrite - 模块实现,在IIS中使用Helicons实现 IIS 5.1 6 ISAPI_Rewrite LiteMicrosoft URL Rewrite Module for IIS 7(或Helicon提供的非免费替代方案之一)。

例如,以下设置将确保现有文件或目录中无法匹配的每个请求都将转移到index.php文件。

mod_rewrite (文档根目录中的.htaccesshttpd.conf中的某个地方

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR] // IF is file (with size > 0)
RewriteCond %{REQUEST_FILENAME} -l [OR] // OR is symbolic link
RewriteCond %{REQUEST_FILENAME} -d      // OR is directory  
RewriteRule ^.*$ - [NC,L]               // DO NOTHING
RewriteRule ^.*$ index.php [NC,L]       // TRANSFER TO index.php

ISAPI_Rewrite Lite (在IIS设置的相应对话框中)

// uses same syntax as mod_rewrite
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Microsoft URL重写模块 (位于文档根目录中的web.config或配置树中的seomewhere)

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
                <rule name="MatchExistingFiles" stopProcessing="true">
                    <match url="^.*$" />
                    <conditions logicalGrouping="MatchAny">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" pattern="" ignoreCase="false" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" pattern="" ignoreCase="false" />
                    </conditions>
                    <action type="None" />
                </rule>
                <rule name="RemapMVC" stopProcessing="true">
                    <match url="^.*$" />
                    <conditions logicalGrouping="MatchAll" />
                    <action type="Rewrite" url="index.php" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>