Remove index.php from {uri} (rewrite)

Currently i’m trying to migrate my nextcloud installation from nginx to caddy.
Is almost works, but some images don’t load. Icons and stuff.

I tracked it down to the rewrite rules.
All links to working and not working images look the same:
Some of them load, some of them get redirected and return nothing.

However, when i remove the index.php part from the images which won’t load, it works.

I’m currently struggling writing the rewrite rule, i tried different things, but non of them seemed to work.
I’m using 1to1 this config: examples/Caddyfile at master · caddyserver/examples · GitHub

It seems like the following nginx rule is missing in this caddy config

    location ~* \.(?:svg|gif|png|html|ttf|woff|ico|jpg|jpeg)$ {
        try_files $uri /index.php$uri$is_args$args;

I tried already something like this:

    rewrite {
        to {uri} {uri}/ /index.php?{uri}

but it just gets worse. I think the main problem is, that index.php is part of uri. Is there any way i can remove it?

Hi @TrAnn3l, welcome to the Caddy community. Your avatar is my favourite PD2 mask!

Rewrite comes with regex capabilities, so you could remove the index.php part that way.

Going off your example:

rewrite {
  if_op or # proceed if any of the following is true:
  if {path} ends_with svg
  if {path} ends_with gif
  if {path} ends_with png
  if {path} ends_with html
  if {path} ends_with ttf
  if {path} ends_with woff
  if {path} ends_with ico
  if {path} ends_with jpg
  if {path} ends_with jpeg
  r ^/index.php/(.+)$
  to /{1}

Hey @Whitestrake, thanks for your help.

It is almost working, only one images is not loading (404 not found):
And again, when i manually remove the index.php it is loading. I have no idea why the rewrite is not triggering.
I put your rewrite first, so there should be nothing intercepting.

In addition, i changed your code a little bit and used the ext directive

Adding /index.php?{1} to the to directive fixed it

My code looks now like: {

	root   /var/www/nextcloud
	log    /var/log/nextcloud_access.log
	errors /var/log/nextcloud_errors.log

	fastcgi / php {
		env PATH /bin

    rewrite {
        ext .svg .gif .png .html .ttf .woff .ico .jpg .jpeg
        r ^/index.php/(.+)$
        to /{1} /index.php?{1}

    rewrite {
		r ^/index.php/.*$
		to /index.php?{query}

	# client support (e.g. os x calendar / contacts)
	redir /.well-known/carddav /remote.php/carddav 301
	redir /.well-known/caldav /remote.php/caldav 301

	# remove trailing / as it causes errors with php-fpm
	rewrite {
		r ^/remote.php/(webdav|caldav|carddav|dav)(\/?)$
		to /remote.php/{1}

	rewrite {
		r ^/remote.php/(webdav|caldav|carddav|dav)/(.+?)(\/?)$
		to /remote.php/{1}/{2}

	rewrite {
		r ^/public.php/(.+?)(\/?)$
		to /public.php/(.+?)(\/?)$

	# .htaccess / data / config / ... shouldn't be accessible from outside
	status 403 {

	header / Strict-Transport-Security "max-age=31536000;"


Till now, nextcloud is the only site which still requires a lot of rewrites.
I will make a PR to update the example.

1 Like

Nice! Can’t believe I forgot about that.

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