Apache 配置

请确保 .htaccessindex.php 这两个文件位于同一个可公开访问的目录中。.htaccess 文件包含以下代码:

  1. RewriteEngine On
  2. RewriteCond %{REQUEST_FILENAME} !-f
  3. RewriteCond %{REQUEST_FILENAME} !-d
  4. RewriteRule ^ index.php [QSA,L]

此外,为了保证 .htaccess 文件中的重写规则(rewrite rules)能够生效,虚拟服务器必须启用 AllowOverride 选项:

  1. AllowOverride All

Nginx 配置

假设 Slim 的 index.php 文件位于项目的根目录(www root),将以下代码和其他需要的设置一起写入到 nginx 配置文件的 location 块中:

  1. try_files $uri $uri/ /index.php?$args;

HHVM

将以下设置代码中的 SourceRoot 修改为 Slim 应用的文档根目录,然后和其他需要的设置一起写入到 HHVM 配置文件中:

  1. Server {
  2. SourceRoot = /path/to/public/directory
  3. }
  4. ServerVariables {
  5. SCRIPT_NAME = /index.php
  6. }
  7. VirtualHost {
  8. * {
  9. Pattern = .*
  10. RewriteRules {
  11. * {
  12. pattern = ^(.*)$
  13. to = index.php/$1
  14. qsa = true
  15. }
  16. }
  17. }
  18. }

IIS

请确保 Web.configindex.php 这两个文件位于同一个可公开访问的目录中。Web.config 文件包含以下代码:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3. <system.webServer>
  4. <rewrite>
  5. <rules>
  6. <rule name="slim" patternSyntax="Wildcard">
  7. <match url="*" />
  8. <conditions>
  9. <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  10. <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
  11. </conditions>
  12. <action type="Rewrite" url="index.php" />
  13. </rule>
  14. </rules>
  15. </rewrite>
  16. </system.webServer>
  17. </configuration>

lighttpd

假设 Slim 的 index.php 文件位于项目的根目录(www root),将以下代码和其他需要的设置一起写入到 lighttpd(>= 1.4.24)配置文件中:

  1. url.rewrite-if-not-file = ("(.*)" => "/index.php/$0")