Wordpress on subdirectory of domain

I’m trying to host wordpress on caddy. I’ve used the configuration but with mixed success:

Attempt #1: Conventional wordpress on subdomain

blog.example.com {
    root /var/www/wordpress
    gzip
    fastcgi / /run/php/php7.0-fpm.sock php
    rewrite {
        if {path} not_match ^\/wp-admin
        to {path} {path}/ /index.php?_url={uri}
    }
}

This works perfectly well, except that it is on a subdomain, which is not what I want to optimally have.

Attempt #2: Wordpress on subdirectory, without rewrite

www.example.com/blog {
    root /var/www/wordpress
    gzip
    fastcgi / /run/php/php7.0-fpm.sock php
}

This works fine as well. Except that I can’t activate pretty permalinks feature on wordpress.

Attempt #3: Wordpress on subdirectory, with rewrite

www.example.com/blog {
    root /var/www/wordpress
    gzip
    fastcgi / /run/php/php7.0-fpm.sock php
    rewrite {
        if {path} not_match ^\/wp-admin
        to {path} {path}/ /index.php?_url={uri}
    }
}

Here’s where the trouble comes in. I am able to display index.php albeit without any css (it couldn’t load). Additionally I can’t access the wp-admin section of the site, etc.

Other variants of rewrite tried include:

rewrite {
        if {path} not_match ^\/blog\/wp-admin
        to {path} {path}/ /index.php?_url={uri}
    }
rewrite {
        if {path} not_match ^\/wp-admin
        to {path} {path}/ /blog/index.php?_url={uri}
    }
rewrite {
        if {path} not_match ^\/blog\/wp-admin
        to {path} {path}/ /blog/index.php?_url={uri}
    }

They don’t work as well. I must have misunderstood some parts of the rewrite function, but I just can’t wrap my head around why the same code cannot work on a subdirectory configuration when it works fine on a normal domain.

Appreciate all the help I can get!

EDIT:

I thought it may be relevant to point out that my domain is currently using this configuration:

www.example.com {
    proxy / localhost:8001 {
        except /static
        except /shop
        except /blog
        transparent
    }
}

Hi @spitfire789,

WordPress, like a lot of apps, assumes by default that they control the entire site root. This is what causes the issue you’re seeing - you get the HTML document, but no styling, scripts, images, etc. because those resources are linked out of the web root instead of the correct subfolder.

I understand that it can be configured to behave properly - the WordPress wiki seems to have some useful information on that front.

https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory#Method_II_.28With_URL_change.29

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