如何处理前端控制器模式中的子目录 [英] How to handle subdirectories in a Front Controller pattern

查看:42
本文介绍了如何处理前端控制器模式中的子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL;DR:如果请求没有文件的子目录,我的 URL 重写会中断.我没有按预期加载默认的 home 页面,而是收到 403 Forbidden 错误.

TL;DR: My URL rewriting breaks if a subdirectory without a file is requested. Instead of loading a default home page as expected, I'm getting a 403 Forbidden error.

我正在使用 Apache URL 重写来构建一个使用 Front Controller 模式的站点.到目前为止,我的 .htaccess 看起来像这样:

I'm using Apache URL rewriting to build a site using the Front Controller pattern. So far, my .htaccess looks like this:

Options -Indexes
RewriteCond %{REQUEST_URI} ^/(subdir|subdir/.*|subdir2|subdir2/.*).*$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+) index.php?request=$1 [QSA]

所以这适用于除子目录请求之外的所有请求:

So this is working for all requests except requests for subdirectories:

  • mydomain.com/ 结果 ->

  • mydomain.com/index.php 带有 home.inc 内容
  • mydomain.com/index.php with home.inc content

mydomain.com/page1 结果 ->

  • mydomain.com/index.php 带有 /pages/page1.inc 内容
  • mydomain.com/index.php with /pages/page1.inc content

mydomain.com/subdir/ 结果 ->

  • 403 Forbidden

mydomain.com/subdir/page1 结果 ->

  • mydomain.com/index.php 带有 /subdir/pages/page1.inc 内容
  • mydomain.com/index.php with /subdir/pages/page1.inc content

下面的更多细节可能无关紧要,因为问题可能出在 .htaccess 中.

More details below that probably don't matter, since the issue is likely in the .htaccess.

index.php 中,我正在捕获请求并使用它从目录 /pages 中获取相应的包含页面,该目录具有以下文件每个页面的内容.这是来自 index.php 的(稍微简化的)代码:

In index.php, I'm catching the request and using that to grab the corresponding include page from a directory /pages which has files with the content of each page. Here's the (somewhat simplified) code from index.php:

//grab the actual HTTP request
$request = $_GET['request'];

//if the request has slashes, isolate the directory part into $dir
$slashPos = strrpos($request, "/");
if($slashPos !== false){
    $dir = substr($request, 0, $slashPos) . "/";
    $page = basename($request, ".inc");
} else {
    $dir = "";
    $page = request;
}

//use "home" if no filename is specified.
if($page==""){$page="home";}

//build path to content include
$content = $dir . "pages/" . $page . ".inc";

//output page
require("header.php"); 
require($content ); 
require("footer.php");

这非常适合根目录.对 mydomain.com/page1 的请求导致 mydomain.com/index.phpmydomain.com/pages/page1 的内容一起提供> 包括.

This works perfect for the root directory. A request for mydomain.com/page1 results in mydomain.com/index.php being served with the contents of mydomain.com/pages/page1 included.

它也适用于子目录中的页面:对 mydomain.com/subdir/page1 的请求会导致 mydomain.com/index.php 包含 mydomain.com/subdir/pages/page1 的内容.

It also works for pages within a subdirectory: a request for mydomain.com/subdir/page1 results in mydomain.com/index.php being served with the contents of mydomain.com/subdir/pages/page1 included.

当请求是针对实际目录时,一切都会中断.所以 mydomain.com/subdir/ 返回 403 Forbidden.为什么要这样做?我希望它加载带有 subdir$dir 和带有 home$page (我设置为默认值)$page=="").

It all breaks when the request is for an actual directory. So mydomain.com/subdir/ returns 403 Forbidden. Why is it doing that? I expect it to load $dir with subdir and $page with home (I set as a default for when $page=="").

是的,Options -Indexes 可能导致 403,但为什么只在子目录上?它在 root 上不是 403.而 .htaccess 中的 RewriteCond %{REQUEST_URI} ^/(subdir|subdir/.*|subdir2|subdir2/.*).*$ 行应该能抓住它,对?

Yes, Options -Indexes is probably causing the 403, but why only on subdirectories? It doesn't 403 on root. And the line RewriteCond %{REQUEST_URI} ^/(subdir|subdir/.*|subdir2|subdir2/.*).*$ in .htaccess should catch it, right?

推荐答案

好的,我想通了.问题是 subdir 实际上不在 web 根目录下.我的整个网站实际上都在它自己的子目录中工作.换句话说,我不在

Ok, I figured it out. The problem is that subdir wasn't actually at web root. My entire site is actually working in its own subdirectory. In other words, I wasn't working in

mydomain.com/

作为根,我在工作

mydomain.com/mydivision/

作为根.很明显在这一行:

as a root. So obviously in this line:

RewriteCond %{REQUEST_URI} ^/(subdir|subdir/.*|subdir2|subdir2/.*).*$

^ 让它在实际的 webroot 中寻找 subdir.为了修复,我删除了 ^ 或将我的实际子域添加到 ^ 之后的那一行.

the ^ made it look for subdir at the actual webroot. To fix I removed ^ or add my actual subdomain to that line after ^.

这篇关于如何处理前端控制器模式中的子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆