Simple website JS and CSS rewrite

Hi all,

I am trying to serve an HTML/CSS/JS static website from Caddy.
I want it to be accessible at 192.168.1.210/website and I am trying with:

192.168.1.210/website {
    root /srv/mywebsite
}

This serves index.html but not the *.js and *.css files which are in the javascript and css directories along index.html. I have read the doc, the examples and tried many rewrites but none works for me so far.

For now I am running an Nginx server to serve the website and Caddy simply proxies to it, which is rather clumsy.

Thanks for the help !!

What are the link and script tags that reference your CSS and JS files?

Hi matt, thanks for the quick reply !

<script src="javascript/startup.js" type="text/javascript" async></script>
<script src="javascript/slideshow.js" type="text/javascript" async></script>
<link href="css/style_page.css" rel="stylesheet" type="text/css" >

If i can leave these to be relative and not absolute paths, that would be better.

Unless the javascript and css folders are in a folder called website, those paths will not work. They have to be relative to the website folder (which may not exist, but that caddy config makes it look real to the client).

1 Like

For now the file structure is:
/srv/
– mywebsite/
– – index.html
– – javascript/
– – – startup.js
– – css/
– – – style.css

The index.html loads but the browser tries to load 192.168.1.210/javascript/startup.js instead of 192.168.1.210/mywebsite/javascript/startup.js. Is it not possible to rewrite the requests somehow with Caddy ?

Anyone has any idea how to rewrite the requests ? I am having this problem with many websites actually so that would really help. Thanks a lot

The only way to intercept these with Caddy is to write another site label that catches non-/website-prefixed URIs, like:

192.168.1.210 {
  redir 301 {
    if_op or
    if {path} starts_with /css
    if {path} starts_with /js
    / {host}/website{uri}
  }
}

But I would probably look at the browser as the culprit for now - the format you used in the HTML example above is relative, and a modern browser should be prepending /website if that’s what’s already in the URL bar.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.