configurationApache以通过单个index.php处理所有请求

我inheritance了丑陋的一块PHP,必须通过单个index.php处理所有请求。

index.php使用聪明的embedded式商业逻辑根据明智的url生成不同的网页。

我如何configurationapache2 / php5通过index.php处理所有请求?

更新:有没有其他的mod-rewrite在这里使用?

好的,没有mod_rewritemod_rewrite当然是答案,但是这个使用了mod_alias

 RedirectMatch permanent ^/(?<!index\.php)(.*) http://example.com/index.php 

这应该匹配任何不是前面index.php并发出一个permanantredirect到正确的index.php。 请注意,您需要指定完整位置作为目标。

更新我已经testing过了,因为它使用PCRE,所以它需要Apache 2.0+。 如果你需要附加到index.php的实际path,那么附加$1到redirectpath。

 LoadModule rewrite_module /usr/lib/apache/modules/mod_rewrite.so RewriteEngine on RewriteCond %{REQUEST_URI} !^/?index.php$ RewriteRule . index.php 

将以下内容保存到Web根目录中名为.htaccess的文件中。

 # Turn on URL rewriting RewriteEngine On # Web Directory RewriteBase / # Protect certain folders from being viewed RewriteRule ^(protected|directories) - [F,L] # Rewrite URLs to index.php/URL RewriteRule .* index.php/$0 [PT,L] 

你可以用mod_rewrite规则来做到这一点。

 #.htaccess code RewriteEngine On RewriteRule ^(.*)$ index.php [L] 

这是什么与我的WordPress的安装:

 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] 

这两个RewriteCond行允许提供现有的文件:没有他们,你必须通过index.php传递图像和样式表。

这比其中的任何一个都要简单得多,而且你需要一个没有mod_rewrite的解决scheme,所以:

  Alias /static/ /path/to/static/files/ Alias / /path/to/script.php/ 

如果你没有静态文件,你当然可以省略第一行(但是它通常会回答“我该怎么做例外”)。

作为旧版Apache(1.x)的一个肮脏的黑客,你可以使用404处理程序和DocumentRoot一个空目录,但这是很丑陋的。 我以前看过这个build议,但那是很久以前的必要了。 这可能有助于在奇怪的情况下。