0

我一直在尝试模仿 nodejs/express 如何使用他们的路由。我将所有流量转发index.php到处理路由(使用AltoRouter)。

我的文件结构是这样的:

-/
--public/
  |- assets
  |- ...
--routes/
  |- route.php
  |- ...
--index.php

以这些 url 为例(都应该返回/重定向 404):

http://testsite.com/routes

http://testsite.com/routes/route.php

http://testsite.com/somefile.php

但是,只有资产应该可以像这样直接访问(我不想包括/public/

http://testsite.com/assets/image.png

http://testsite.com/assets/styles/style.css

这是我到目前为止所拥有的:

# Make sure mod_rewrite is on
<IfModule mod_rewrite.c>
    RewriteEngine on

    # This should allow us to get our assets and keep them public
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)\.(gif|jpg|png|jpeg|css|js|swf)$ /public/$1.$2 [L,NC]

    # Forward other traffic to index.php
    RewriteRule ^.+$ index.php [L]
</IfModule>

我遇到的一个问题是:http://testsite.com/routes产生: 在此处输入图像描述

我想我的主要问题是任何不公开的东西都不能访问(不确定 .htacces 是否可行)

4

1 回答 1

1

如果您使用正确的规则,则不必将文件埋在 Web 根目录之上。私人文件很容易变得无法访问。

<IfModule mod_rewrite.c>
    RewriteEngine on
    # transform assets URLs to correct path, and proceed to next rule
    # checking existence in RewriteConds is useless here since public URLs are not 1:1 physical paths
    RewriteRule ^assets/.+ public/$0 [NS,DPI]
    # send *all* URLs to index.php except those that point to an asset file that exists
    RewriteCond $1 !=public/assets/ [OR]
    # change the slash in next condition to your sub-directory if not serving from the web root, e.g. %{DOCUMENT_ROOT}/project/root/$0
    RewriteCond %{DOCUMENT_ROOT}/$0 !-f
    RewriteRule ^((?:[^/]+/){2})?.+ index.php [NS,END]
</IfModule>
于 2016-11-06T11:17:54.353 回答