<p>Put all public assets inside the web root (<code>htdocs</code> in your folder structure). Everything else gets complicated and opens you up to accidentally exposing files you don't want to be publicly accessible.</p>
<p>We use a build script and only the output (CSS, JS, other assets) are placed in the web root by the build script. The source files live outside the web root and are not accessible. If you don't have a build step, just place your assets directly in the web root.</p>
<blockquote>
<p>If you put <code>about/assets</code> in the document root, craft cannot refer to <code>templates/about/index.twig</code>.</p>
</blockquote>
<p>Not sure what the placement of your assets has to do with the path to a template? In any case, you can configure the path to your templates in your <a href="https://craftcms.com/docs/5.x/configure.html#entry-script" rel="nofollow noreferrer">entry script</a> using the constant <a href="https://craftcms.com/docs/5.x/reference/config/bootstrap.html#craft-templates-path" rel="nofollow noreferrer"><code>CRAFT_TEMPLATES_PATH</code></a>.</p>

CC BY-SA 4.0
2024-06-12T03:41:13.890Z
For the configuration in the example I gave,   
[https://dummy.jp/about/](https://dummy.jp/about/) will return a 403 error.  

This is the correct behaviour for a .htaccess structure.  
However, some coding rules can create unintended behaviour, so I would like to find out what is the best way to do this for the structure I have raised the issue with.
shin_develop
CC BY-SA 4.0
2024-06-12T09:50:11.587Z
@shin_develop I don't understand what problem you're trying to solve.
MoritzLost
CC BY-SA 4.0
2024-06-07T07:47:41.907Z
2024-06-07T07:47:41.907Z
MoritzLost
<p>We are exploring best practice for referencing subdirectory js and css in html path references.</p>
<p>See below.</p>
<p><a href="https://dummy.jp/about/" rel="nofollow noreferrer">https://dummy.jp/about/</a></p>
<p>directory structure looks like this.</p>
<pre><code>┣ craft/
┃  ┣ various_dirs/
┃  ┗ templates/
┃    ┣ index.twig
┃    ┗ about/
┃      ┗ index.twig
┗ htdocs/
  ┣ cpresources/
  ┣ index.php
  ┣ .htaccess
  ┣ assets/
  ┗ about/
    ┗ assets/
      ┣ js/
      ┣ css/
      ┗ images/

</code></pre>
<p>As a coding rule this time, the assets for each page must be placed under that path.</p>
<p>js/css reference in <code>about/index.twig</code> looks like this.</p>
<p><code>&lt;script src=&quot;/about/assets/js/about.min.js&quot;&gt;&lt;/script&gt;</code></p>
<p>If you put <code>about/assets</code> in the document root, craft cannot refer to templates/about/index.twig.<br />
(This is because it does not satisfy <code>RewriteCond %{REQUEST_FILENAME} ! -d</code> )</p>
<p>To avoid this, it is commented out to refer to the assets in the document root</p>
<pre><code>&lt;IfModule mod_rewrite.c&gt;
    RewriteEngine On.

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} ! -f
# RewriteCond %{REQUEST_FILENAME} ! -d
    RewriteCond %{REQUEST_URI} ! ^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (. +) index.php?p=$1 [QSA,L]
&lt;/IfModule&gt;
</code></pre>
<p>However, I have a feeling this method is not a very good way of doing things...
How are assets best placed?</p>
<hr />
<p><em>Edit (by Moderator) - Answer from Author converted to Edit on original post:</em></p>
<p>For example, this way</p>
<pre><code>    RewriteEngine On

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d [NC,OR] # ←add
#↓add
    RewriteCond %{REQUEST_URI} (.+)/$
#↑add
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]
&lt;/IfModule&gt;```
</code></pre>

<settings>
References to static assets placed under subfolders
# References to static assets placed under subfolders

(Asked by: username_0 on 2024-06-07)

We are exploring best practice for referencing subdirectory js and css in html path references.
See below.
https://dummy.jp/about/
directory structure looks like this.
┣ craft/
┃  ┣ various_dirs/
┃  ┗ templates/
┃    ┣ index.twig
┃    ┗ about/
┃      ┗ index.twig
┗ htdocs/
  ┣ cpresources/
  ┣ index.php
  ┣ .htaccess
  ┣ assets/
  ┗ about/
    ┗ assets/
      ┣ js/
      ┣ css/
      ┗ images/


As a coding rule this time, the assets for each page must be placed under that path.
js/css reference in about/index.twig looks like this.

If you put about/assets in the document root, craft cannot refer to templates/about/index.twig.
(This is because it does not satisfy RewriteCond %{REQUEST_FILENAME} ! -d )
To avoid this, it is commented out to refer to the assets in the document root

    RewriteEngine On.

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} ! -f
# RewriteCond %{REQUEST_FILENAME} ! -d
    RewriteCond %{REQUEST_URI} ! ^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (. +) index.php?p=$1 [QSA,L]


However, I have a feeling this method is not a very good way of doing things...
How are assets best placed?

Edit (by Moderator) - Answer from Author converted to Edit on original post:
For example, this way
    RewriteEngine On

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d [NC,OR] # ←add
#↓add
    RewriteCond %{REQUEST_URI} (.+)/$
#↑add
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]
```

**Tags:** settings

---

## 1 answers

### Answer by username_1 on 2024-06-07. Score: 1

Put all public assets inside the web root (`htdocs` in your folder structure). Everything else gets complicated and opens you up to accidentally exposing files you don't want to be publicly accessible.

We use a build script and only the output (CSS, JS, other assets) are placed in the web root by the build script. The source files live outside the web root and are not accessible. If you don't have a build step, just place your assets directly in the web root.

> If you put `about/assets` in the document root, craft cannot refer to `templates/about/index.twig`.

Not sure what the placement of your assets has to do with the path to a template? In any case, you can configure the path to your templates in your [entry script](https://craftcms.com/docs/5.x/configure.html#entry-script) using the constant [`CRAFT_TEMPLATES_PATH`](https://craftcms.com/docs/5.x/reference/config/bootstrap.html#craft-templates-path).

> **Comments:**
> **username_0**: For the configuration in the example I gave,    [https://dummy.jp/about/](https://dummy.jp/about/) will return a 403 error.    This is the correct behaviour for a .htaccess structure.   However, some coding rules can create unintended behaviour, so I would like to find out what is the best way to do this for the structure I have raised the issue with.  
> **username_1**: @shin_develop I don't understand what problem you're trying to solve.  

---

