将不存在的目录作为 IIS 或 Apache 中的参数传递
-
19-09-2019 - |
题
首先看这个网址:
https://stackoverflow.com/questions/tagged/xoxoxo/
该目录不存在,但不知何故 stackoverflow 可以将最后一个目录作为参数传递给他的基本脚本。
是否可以配置 IIS 或 Apache 来执行此操作?如何?
解决方案
这种行为背后的机制称为 url重写 并可以实施于 阿帕奇 与 mod_rewrite
-模块并在 IIS 中使用任一 Helicons ISAPI_Rewrite Lite (或 Helicon 提供的非免费替代方案之一) IIS 5.1 和 6 或与 微软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
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]
微软URL重写模块 (在你的 web.config
在文档根目录或配置树中的某个位置)
<?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>
不隶属于 StackOverflow