Apache PHP项目根处理程序

时间:2014-05-01 22:58:44

标签: php apache web

我在本地运行apache 24使用php.My目标是每次我发出url请求它将链接到项目文件夹的根目录http://localhost/project_root/

示例:

http://localhost/project_root/是输入链接http://localhost/project_root/index.php

第二个例子:

http://localhost/project_root/foo/bar会链接http://localhost/project_root/index.php

最终的最终想法是http://localhost/project_root/index.php处理重定向和加载资源。

提前致谢,欢迎提出任何建议。

2 个答案:

答案 0 :(得分:3)

对于这种事情,我将.htaccess文件放在应用程序的根目录中:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d  #Optional: Only if it's not a valid directory
RewriteCond %{REQUEST_FILENAME} !-f  #Only if it's not a valid file
RewriteRule (.*) index.php?q=$1 [L,QSA]

这会将网址的剩余部分传递为$_GET['q'],并保留其他任何参数。

您需要确保在Apache中启用mod_rewrite,并且在您的网站启用中将AllowOverride设置为all

答案 1 :(得分:2)

您正在寻找的是实施前控制器 http://www.sitepoint.com/front-controller-pattern-1/

为此,您需要使用mod_rewrite强制所有.php请求转到前端控制器。

以下内容可以在您的.htaccess或apache配置(httpd.conf)

中找到
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ project_root/index.php