"browse" sub-directory broken URL

(b2c) #1

When I use browse and rewrite both together.

browse /files/ 

rewrite {
	if {path} not_starts_with /api 
		to {path} {path}/ /
}

While I’m browsing localhost/files, I click on a directory from file list -

href="./Directory_One/" point to localhost/Directory_One

It should be pointed to localhost/files/Directory_One

If I remove rewrite rules, it works perfectly. What’s wrong here?

(Toby Allen) #2

What url are you on when you click on the link?

It may be that rewrite is losing the . from ./

(b2c) #3

@tobya

I’m in the root of browse when I click on it: localhost/files

(Matthew Fay) #4

Caddy generates relative links, e.g. ./Directory_One/. Your browser is meant to append this relative link to the current directory.

When you browse to localhost/files, you think that the current directory is /files, thus you are looking at the index of that directory, and the relative link should lead to /files/Directory_One/. Incidentally, Caddy thinks the same way - because of your rewrite, it doesn’t need a trailing slash.

But the browser disagrees. The browser thinks the current directory is /, and you’re looking at a page called files. Thus . goes up to the current directory and you end up at the absolute path /Directory_One/.

The solution is to browse to localhost/files/ (note the trailing slash), so your browser recognizes that it’s in a subfolder.

The reason localhost/files works at all is because you are rewriting to {path}/. Normally a request to localhost/files wouldn’t meet the browse /files/ base path requirement.

2 Likes
(b2c) #5

Yes you’re right. Trailing slash is a MUST. Though visitors may miss it sometimes!

For now I’ve edited JS file of custom tpl to solve it.

browse /files C:\Caddy\style.tpl

and javascript to force trailing slash

history.pushState({}, null, window.location.href.replace(//?$/, ‘/’));