先看这个网址:
https://stackoverflow.com/questions/tagged/xoxoxo/
该目录不存在,但stackoverflow可以将最后一个目录作为参数传递给他的基本脚本。
这是否可以配置 IIS 或 Apache 来做到这一点?如何?
先看这个网址:
https://stackoverflow.com/questions/tagged/xoxoxo/
该目录不存在,但stackoverflow可以将最后一个目录作为参数传递给他的基本脚本。
这是否可以配置 IIS 或 Apache 来做到这一点?如何?
这种行为背后的机制称为url 重写,可以在Apache中使用mod_rewrite
-modules 和在 IIS 中使用 Helicons ISAPI_Rewrite Lite(或 Helicon 提供的非免费替代方案之一)用于IIS 5.1和6或使用IIS 7的Microsoft URL 重写模块。
例如,以下设置将确保在现有文件或目录上无法匹配的每个请求都将传输到该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
的文档根目录或配置树中的某个位置)
<?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>