在 .htaccess 中模拟 Node/Express 路由
Emulating Node/Express Routes in .htaccess
我一直在尝试模仿 nodejs/express 如何使用他们的路线。我将所有流量转发到 index.php
以处理路由(使用 AltoRouter)。
我的文件结构是这样的:
-/
--public/
|- assets
|- ...
--routes/
|- route.php
|- ...
--index.php
以这些url为例(都应该return/redirect 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/. [L,NC]
# Forward other traffic to index.php
RewriteRule ^.+$ index.php [L]
</IfModule>
我遇到的一个问题是:http://testsite.com/routes
产生:
我想我的主要问题是 public 中没有的任何东西都不应该可以访问(不确定 .htacces 是否可行)
如果您使用正确的规则,则不必将您的文件埋在网络根目录之上。私有文件很容易被设置为不可访问。
<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/[=10=] [NS,DPI]
# send *all* URLs to index.php except those that point to an asset file that exists
RewriteCond !=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/[=10=]
RewriteCond %{DOCUMENT_ROOT}/[=10=] !-f
RewriteRule ^((?:[^/]+/){2})?.+ index.php [NS,END]
</IfModule>
我一直在尝试模仿 nodejs/express 如何使用他们的路线。我将所有流量转发到 index.php
以处理路由(使用 AltoRouter)。
我的文件结构是这样的:
-/
--public/
|- assets
|- ...
--routes/
|- route.php
|- ...
--index.php
以这些url为例(都应该return/redirect 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/. [L,NC]
# Forward other traffic to index.php
RewriteRule ^.+$ index.php [L]
</IfModule>
我遇到的一个问题是:http://testsite.com/routes
产生:
我想我的主要问题是 public 中没有的任何东西都不应该可以访问(不确定 .htacces 是否可行)
如果您使用正确的规则,则不必将您的文件埋在网络根目录之上。私有文件很容易被设置为不可访问。
<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/[=10=] [NS,DPI]
# send *all* URLs to index.php except those that point to an asset file that exists
RewriteCond !=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/[=10=]
RewriteCond %{DOCUMENT_ROOT}/[=10=] !-f
RewriteRule ^((?:[^/]+/){2})?.+ index.php [NS,END]
</IfModule>