将pagerequests重写到index.php,将filerequests重写到app/webroot目录

发布于 2024-09-09 09:42:50 字数 1066 浏览 1 评论 0原文

嘿,我已经阅读 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒江雪… 2024-09-16 09:42:50

看看这是否更符合您的预期:

RewriteEngine On

# These two lines are very specific to your current setup, to prevent
# mod_dir from doing what it does, but in a more controlled way
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/iceberg[^/]
RewriteRule .* http://localhost/iceberg/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI}      !^/app/webroot
RewriteCond %{REQUEST_URI}       \.[a-z]+$ [NC]
RewriteRule ^.*$ app/webroot/$0 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI}      !^/app/webroot
RewriteRule ^.*$ index.php?route=$0 [QSA,L]

另外,解释一下,您收到 500 错误的原因可能是因为您的规则:

RewriteRule ^(.*)$ index.php?route=$1 [QSA,L]

由于它是无条件的,并且正则表达式模式将始终匹配,因此您的重写将是一遍又一遍地执行(L 标志并不能阻止这种情况,因为在重写到 index.php 后,Apache 内部会进行内部重定向,并且进程会丢失当前状态)。

See if this works out a little more like you expected:

RewriteEngine On

# These two lines are very specific to your current setup, to prevent
# mod_dir from doing what it does, but in a more controlled way
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/iceberg[^/]
RewriteRule .* http://localhost/iceberg/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI}      !^/app/webroot
RewriteCond %{REQUEST_URI}       \.[a-z]+$ [NC]
RewriteRule ^.*$ app/webroot/$0 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI}      !^/app/webroot
RewriteRule ^.*$ index.php?route=$0 [QSA,L]

Also, to explain, the reason why you are getting the 500 error is likely because of your rule:

RewriteRule ^(.*)$ index.php?route=$1 [QSA,L]

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 to index.php, an internal redirection is made inside of Apache, and the process loses its current state).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文