CakePHP [.htaccess],静态文件作为项目的根

时间:2023-02-05 12:51:28

I would like to be able to access static files at the root of my project, in a folder at the same level of app/. It's because that is the only directory to wich I have the permission to read-write files on my server, so our images are uploaded there.

我希望能够在我的项目的根目录,在相同级别的app /的文件夹中访问静态文件。这是因为这是我唯一有权在我的服务器上读写文件的目录,因此我们的图像会上传到那里。

So if someone writes this URL:

所以如果有人写这个URL:

www.mysite.com/img-rw

It displays images in the folder at [project-root]/my-rw-dir

它在[project-root] / my-rw-dir的文件夹中显示图像

Any ideas on how to edit .htaccess files for something like this to be done?

有关如何编辑.htaccess文件的任何想法都可以完成吗?

Thanks

2 个解决方案

#1


Taking into account that this is a course project (as the OP said in comments) and you are NOT concerned about security flaws, here is what you could do.

考虑到这是一个课程项目(正如OP在评论中所说的那样)并且您并不担心安全漏洞,这是您可以做的。

By default CakePHP has 3 nested .htaccess files:

默认情况下,CakePHP有3个嵌套的.htaccess文件:

  1. /.htaccess (Project root folder)
  2. /.htaccess(项目根文件夹)

  3. /app/.htaccess
  4. /app/webroot/.htaccess

The first (topmost) of them is the first to respond when you try to access www.mysite.com:

当您尝试访问www.mysite.com时,第一个(最顶层)是第一个响应:

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]
</IfModule>

Clearly, this sends the user request right into the /app/webroot folder. We could use the conditional check that exists in /app/webroot/.htacess to, instead of promptly redirecting to webroot, check if the given URL matches to a file or folder. In order to do so, you'll need to update the parent (/.htaccess) file:

显然,这会将用户请求权限发送到/ app / webroot文件夹。我们可以使用/app/webroot/.htacess中存在的条件检查,而不是立即重定向到webroot,检查给定的URL是否与文件或文件夹匹配。为此,您需要更新父(/.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/$1 [L]
</IfModule>

This way, the request will only be redirected to /app/webroot if none of the conditions are met. If you need only to return files, than you could remove RewriteCond %{REQUEST_FILENAME} !-d, which verifies if the URL matches to a folder ( -d = directory).

这样,如果没有满足任何条件,请求将仅重定向到/ app / webroot。如果只需要返回文件,则可以删除RewriteCond%{REQUEST_FILENAME}!-d,它会验证URL是否与文件夹(-d =目录)匹配。

A cleaner version that would only check for files should look like this:

只检查文件的更干净版本应如下所示:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule    (.*) app/webroot/$1 [L]
</IfModule>

With this tweak, accessing www.mysite.com/image.jpg should return the file image.jpg hosted at your project root (/image.jpg).

通过此调整,访问www.mysite.com/image.jpg应该返回在项目根目录(/image.jpg)上托管的文件image.jpg。

#2


For anyone wondering, this works great :

对于任何想知道的人来说,这很有用:

.htaccess (project root)

.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/$1 [L]

    # Everything else is handled by cakephp webroot
    RewriteCond %{REQUEST_URI} !(dir-at-root-level) [NC]
    RewriteRule    ^(.*) app/webroot/$1 [L]
</IfModule>

#1


Taking into account that this is a course project (as the OP said in comments) and you are NOT concerned about security flaws, here is what you could do.

考虑到这是一个课程项目(正如OP在评论中所说的那样)并且您并不担心安全漏洞,这是您可以做的。

By default CakePHP has 3 nested .htaccess files:

默认情况下,CakePHP有3个嵌套的.htaccess文件:

  1. /.htaccess (Project root folder)
  2. /.htaccess(项目根文件夹)

  3. /app/.htaccess
  4. /app/webroot/.htaccess

The first (topmost) of them is the first to respond when you try to access www.mysite.com:

当您尝试访问www.mysite.com时,第一个(最顶层)是第一个响应:

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]
</IfModule>

Clearly, this sends the user request right into the /app/webroot folder. We could use the conditional check that exists in /app/webroot/.htacess to, instead of promptly redirecting to webroot, check if the given URL matches to a file or folder. In order to do so, you'll need to update the parent (/.htaccess) file:

显然,这会将用户请求权限发送到/ app / webroot文件夹。我们可以使用/app/webroot/.htacess中存在的条件检查,而不是立即重定向到webroot,检查给定的URL是否与文件或文件夹匹配。为此,您需要更新父(/.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/$1 [L]
</IfModule>

This way, the request will only be redirected to /app/webroot if none of the conditions are met. If you need only to return files, than you could remove RewriteCond %{REQUEST_FILENAME} !-d, which verifies if the URL matches to a folder ( -d = directory).

这样,如果没有满足任何条件,请求将仅重定向到/ app / webroot。如果只需要返回文件,则可以删除RewriteCond%{REQUEST_FILENAME}!-d,它会验证URL是否与文件夹(-d =目录)匹配。

A cleaner version that would only check for files should look like this:

只检查文件的更干净版本应如下所示:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule    (.*) app/webroot/$1 [L]
</IfModule>

With this tweak, accessing www.mysite.com/image.jpg should return the file image.jpg hosted at your project root (/image.jpg).

通过此调整,访问www.mysite.com/image.jpg应该返回在项目根目录(/image.jpg)上托管的文件image.jpg。

#2


For anyone wondering, this works great :

对于任何想知道的人来说,这很有用:

.htaccess (project root)

.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/$1 [L]

    # Everything else is handled by cakephp webroot
    RewriteCond %{REQUEST_URI} !(dir-at-root-level) [NC]
    RewriteRule    ^(.*) app/webroot/$1 [L]
</IfModule>