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

EBA*_*BAG 1 apache iis mod-rewrite isapi-rewrite url-rewriting

首先看看这个网址:

/sf/ask/tagged/xoxoxo/

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

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

Ste*_*rig 5

这种行为背后的机制称为url-rewriting,可以在Apache中使用mod_rewrite-modules实现,在IIS中使用Helicons ISAPI_Rewrite Lite(或Helicon提供的非免费替代品之一)在IIS 5.16中实现在微软URL重写模块IIS 7.

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

mod_rewrite(.htaccess在您的文档根目录或您的某个位置httpd.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
Run Code Online (Sandbox Code Playgroud)

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]
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)