首先看一下这个网址:
https://stackoverflow.com/questions/tagged/xoxoxo/
此目录不存在,但不知何故stackoverflow可以将最后一个目录作为参数传递给他的基本脚本。
这可以配置IIS或Apache吗?怎么样?
答案 0 :(得分:5)
这种行为背后的机制称为 url-rewriting ,可以在 Apache 中使用mod_rewrite
- 模块实现,在IIS中使用Helicons实现 IIS 5.1 和 6 或ISAPI_Rewrite Lite的Microsoft URL Rewrite Module for IIS 7(或Helicon提供的非免费替代方案之一)。
例如,以下设置将确保现有文件或目录中无法匹配的每个请求都将转移到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]
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>