1

I'm working on a project using Cloud9 IDE, running PHP5 on Apache 2.0. I'm primarily a front-end developer, but I have decent experience with PHP. I'm familiar with MVC frameworks, and to a lesser degree, this functionality is what I'm trying to emulate, but simpler. I tried implementing CakePHP, but found it was a little too robust for what I needed -- I don't want a backend-heavy setup. I want to write some custom sort of routing mechanism for my application.

Ideally, I would like every request to my site to come through one page (this custom "Controller"), and from there I can write my own logic to figure out the appropriate templates, http codes, errors, etc., to include. My question is, how do I make this happen? In other words, how do I make a request to http://mysite.c9.io/user/view/2 get channeled through http://mysite.c9.io/index.php , and not try to request the /user/view/2 directory on my server?

I'm vaguely familiar with mod_rewrite and .htaccess rules, but I suspect they may play a role here.

4

1 回答 1

1

首先确保mod_rewrite已启用。检查你的httpd.conf文件

 LoadModule rewrite_module modules/mod_rewrite.so

#确保它前面没有禁用。接下来将根<Directory>设置更改为

<Directory />
  Options All
  AllowOverride All
</Directory>

确保更改所有出现的AllowOverride Noneto All。然后重新启动 Apache。

现在mod_rewrite已启用,将其添加到.htaccessWeb 根/目录中的文件中

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} !^/index\.php$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^ index.php [L]

这确保了每个本来应该是的请求404(这意味着它不包括图像、css、js 等)现在都通过前端控制器路由index.php。一些内容管理系统喜欢添加另一个%{REQUEST_URI}检查以确保index.php仅调用以处理框架实际期望的请求类型。

Joomla,例如,添加以下内容:

RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf))$ [NC]
于 2013-08-23T03:49:43.637 回答