Apache/Mod 重写:将所有内容路由到 index.php


Apache/Mod Rewrite: Route everything to index.php?

我正在编写一个前端控制器,它将处理页面请求,通过解析

$_SERVER['REQUEST_URI'].

我想将所有请求路由到/index.php

一些示例如下:

example.com/page2?foo=bar
example.com/page2/?foo=bar
example.com/page/action/123/
example.com/page/action/123

我必须在 .htaccess 中编写的正则表达式规则是什么?

我是否需要一个额外的规则才能 example.com/images/目录可以像"正常"目录一样工作?

你可以尝试这样的事情:

RewriteCond %{REQUEST_URI} !^/dir_under_rootdocument/index'.php
RewriteCond %{REQUEST_URI} !'.(gif|jpg|png|js|css)$
RewriteRule (.*) dir_under_rootdocument/index.php/$1 [L,QSA]