I am currently using Caddy to host a single-page application (SPA -
Angular2). After doing some googling, I arrived at the following Caddyfile configuration:
0.0.0.0 {
root /srv
proxy /api goapi:3000
rewrite {
if {path} not_match ^/api
to {path} {path}/ /index.html
}
}
My issue occurs when I click on a link that takes me to the following web-address: www.webname.com/register. When I refresh that page, I expect it to stay on the same page (e.g. register page). What is happening is that I am redirected to the homepage (e.g. www.webname.com).
Are there ways to configure the Caddyfile so that when I hit refresh, I stay on the same page (register page)?
I have seen a different approach that Angular2 provides where it requires one to use the hash-tag but I don’t want to have hashes in my URL.
Your link took me to some search site, looks like the domain might be parked?
Your rewrite means that a request to example.com:2015/register should, in order:
Attempt to serve the file, /srv/register
Attempt to serve a file called index or default with file extensions .html, .htm, or .txt in the folder /srv/register/
Failing the above, serve /srv/index.html
Now, if Caddy can do options 1 or 2, it always will, regardless of whether the page was navigated to by link or refreshed.
If it must fall back to option 3, the request will be handled by Caddy’s static fileserver. It will be treated as a request for /index.html, and the server will return a 301 redirect to /, which is the correct, canonical way to request an index file. That does not explain the discrepany between the ability to navigate by link vs. refreshing the page, though.
When it falls back to option 3, what I am actually getting is the http-status: 304 NOT MODIFIED. Which I think still causes a redirect to “/” like you said. I verified this through Chrome’s dev-tools.
What I would like for to happen is that it stays on “/register” so that the user doesn’t have to navigate back to “/register” from “/”.
So, now, I’m beginning to think that this may be an Angular2 issue and not a Caddy issue because Caddy is doing what it should be doing: serving the static files – mainly: index.html.
This happens because a request to /index.html is redirected to /. So a request to /whatever which gets rewritten to /index.html will also redirect to /.