将pagerequests重写到index.php,将filerequests重写到app/webroot目录
嘿,我已经阅读 StackOverflow.com 很长时间了,但决定注册来提问。我正在编写自己的轻量级 MVC 框架,用于在 index.php 中路由页面请求。
页面请求看起来像/controller/action/arg1/arg2/arg3,它们应该重写为index.php?route=[request]。因此,像 site.com/user/profile/123 这样的[请求]应该重写为index.php?route=user/profile/123
但是,文件并不意味着重写为index.php。图像和样式表等资源位于 /app/webroot/ 文件夹中,不需要执行 PHP。因此,mod_rewrite 引擎应该将所有文件请求重写到 /app/webroot/,并在文件不存在时提供配置的 404 ErrorDocument。
目录结构
- ./index.php
- ./app/webroot/scripts/helpers/hamster.js
- ./app/webroot/images/logo.png
- ./app/webroot/style/main.css
自您可以仅通过文件扩展名 / 点的存在来区分文件请求 (/squirrel.png) 和页面请求 (/user/profile/123) 之间的区别,我希望这会非常简单。但是......我真的很难受,我希望有人能帮助我。
我尝试过的事情是...
重写引擎开启
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ app/webroot/$1 [L]
RewriteRule ^(.*)$ index.php?route=$1 [QSA,L]
...但除了正确重定向到现有文件之外,它并没有真正起作用。页面请求或不存在的文件会导致 HTTP 500 错误。
非常感谢任何帮助! =)
Hey, I've been reading StackOverflow.com for a long time but decided to sign up to ask a question. I'm writing my own lightweight MVC framework that routes page requests in index.php.
Page requests look like /controller/action/arg1/arg2/arg3, and they should be rewritten to index.php?route=[request]. So, a [request] like site.com/user/profile/123 should be rewritten to index.php?route=user/profile/123
However, files aren't meant to rewrite to index.php. Assets such as images and stylesheets are in the /app/webroot/ folder, and don't need PHP to be executed. So, the mod_rewrite engine should rewrite any filerequests to /app/webroot/, and serve the configured 404 ErrorDocument when the file doesn't exist.
Directory structure
- ./index.php
- ./app/webroot/scripts/helpers/hamster.js
- ./app/webroot/images/logo.png
- ./app/webroot/style/main.css
Since you can tell the difference between a file request (/squirrel.png) and a page request (/user/profile/123) just by the existence of the file extension / dot, I was expecting that this would be really easy. But... I'm having a really hard time with it and I was hoping someone could help me out.
Something I've tried was...
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ app/webroot/$1 [L]
RewriteRule ^(.*)$ index.php?route=$1 [QSA,L]
... but it doesn't really work except for redirecting correctly to existing files. Pagerequests or nonexisting files result in HTTP 500 errors.
Any help is greatly appreciated! =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看这是否更符合您的预期:
另外,解释一下,您收到 500 错误的原因可能是因为您的规则:
由于它是无条件的,并且正则表达式模式将始终匹配,因此您的重写将是一遍又一遍地执行(
L
标志并不能阻止这种情况,因为在重写到index.php
后,Apache 内部会进行内部重定向,并且进程会丢失当前状态)。See if this works out a little more like you expected:
Also, to explain, the reason why you are getting the 500 error is likely because of your rule:
Since it's unconditional, and the regular expression pattern will always match, your rewrite will be performed over and over (the
L
flag doesn't prevent this, because after you rewrite toindex.php
, an internal redirection is made inside of Apache, and the process loses its current state).