CakePHP [.htaccess],静态文件作为项目的根
CakePHP [.htaccess], static files as the root of project
我希望能够访问我项目根目录下的静态文件,该文件位于与 app/ 同一级别的文件夹中。这是因为这是我唯一有权在我的服务器上读写文件的目录,所以我们的图像上传到那里。
所以如果有人写这个 URL:
www.mysite.com/img-rw
它显示 [project-root]/my-rw-dir
文件夹中的图像
关于如何编辑 .htaccess 文件以完成这样的事情有什么想法吗?
谢谢
考虑到这是一个课程项目(如 OP 在评论中所说),您不担心安全漏洞 ,这是你可以做的。
默认情况下,CakePHP 有 3 个嵌套的 .htaccess
个文件:
/.htaccess
(项目根文件夹)
/app/.htaccess
/app/webroot/.htaccess
当您尝试访问时,它们中的第一个(最上面的)是第一个响应的 www.mysite.com:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/ [L]
</IfModule>
显然,这会将用户请求直接发送到 /app/webroot
文件夹中。我们可以使用 /app/webroot/.htacess
中存在的条件检查来检查给定的 URL 是否与文件或文件夹匹配,而不是立即重定向到 webroot
。为此,您需要更新父 (/.htaccess
) 文件:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d # Checks if the given URL matches to a folder
RewriteCond %{REQUEST_FILENAME} !-f # Checks if the given URL matches to a file
#RewriteRule ^$ app/webroot/ [L] # This line is commented, should be removed
RewriteRule (.*) app/webroot/ [L]
</IfModule>
这样,只有满足none个条件,请求才会被重定向到/app/webroot。如果您只需要 return 个文件,那么您可以删除 RewriteCond %{REQUEST_FILENAME} !-d
,它会验证 URL 是否与文件夹匹配(-d
= 目录)。
只检查文件的清洁版本应该如下所示:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) app/webroot/ [L]
</IfModule>
通过此调整,访问 www.mysite.com/image.jpg 应该 return 文件 image.jpg
托管在您的项目根目录 (/image.jpg
)。
对于任何想知道的人来说,这很好用:
.htaccess(项目根目录)
<IfModule mod_rewrite.c>
RewriteEngine on
# This folder is a subfolder of project root but contains static images
# Accessed like so: www.mysite.com/imgrw/[file name]
RewriteRule ^imgrw/(.*)$ /dir-at-root-level/img/ [L]
# Everything else is handled by cakephp webroot
RewriteCond %{REQUEST_URI} !(dir-at-root-level) [NC]
RewriteRule ^(.*) app/webroot/ [L]
</IfModule>
我希望能够访问我项目根目录下的静态文件,该文件位于与 app/ 同一级别的文件夹中。这是因为这是我唯一有权在我的服务器上读写文件的目录,所以我们的图像上传到那里。
所以如果有人写这个 URL:
www.mysite.com/img-rw
它显示 [project-root]/my-rw-dir
文件夹中的图像关于如何编辑 .htaccess 文件以完成这样的事情有什么想法吗?
谢谢
考虑到这是一个课程项目(如 OP 在评论中所说),您不担心安全漏洞 ,这是你可以做的。
默认情况下,CakePHP 有 3 个嵌套的 .htaccess
个文件:
/.htaccess
(项目根文件夹)/app/.htaccess
/app/webroot/.htaccess
当您尝试访问时,它们中的第一个(最上面的)是第一个响应的 www.mysite.com:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/ [L]
</IfModule>
显然,这会将用户请求直接发送到 /app/webroot
文件夹中。我们可以使用 /app/webroot/.htacess
中存在的条件检查来检查给定的 URL 是否与文件或文件夹匹配,而不是立即重定向到 webroot
。为此,您需要更新父 (/.htaccess
) 文件:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d # Checks if the given URL matches to a folder
RewriteCond %{REQUEST_FILENAME} !-f # Checks if the given URL matches to a file
#RewriteRule ^$ app/webroot/ [L] # This line is commented, should be removed
RewriteRule (.*) app/webroot/ [L]
</IfModule>
这样,只有满足none个条件,请求才会被重定向到/app/webroot。如果您只需要 return 个文件,那么您可以删除 RewriteCond %{REQUEST_FILENAME} !-d
,它会验证 URL 是否与文件夹匹配(-d
= 目录)。
只检查文件的清洁版本应该如下所示:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) app/webroot/ [L]
</IfModule>
通过此调整,访问 www.mysite.com/image.jpg 应该 return 文件 image.jpg
托管在您的项目根目录 (/image.jpg
)。
对于任何想知道的人来说,这很好用:
.htaccess(项目根目录)
<IfModule mod_rewrite.c>
RewriteEngine on
# This folder is a subfolder of project root but contains static images
# Accessed like so: www.mysite.com/imgrw/[file name]
RewriteRule ^imgrw/(.*)$ /dir-at-root-level/img/ [L]
# Everything else is handled by cakephp webroot
RewriteCond %{REQUEST_URI} !(dir-at-root-level) [NC]
RewriteRule ^(.*) app/webroot/ [L]
</IfModule>