'Can’t access admin when WordPress is in a subfolder

I installed WordPress using this Docker image. I installed WordPress in a sub-folder (https://thibault.vlacich.fr/blog).

I’m using Traefik for routing. Here’s my docker-compose file:

version: '3.7'

networks:
  http_network:
    external: true
  thibaultvlacich:
    external: false

services:
  database:
    image: mariadb:latest
    command: ['--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci', '--default-authentication-plugin=mysql_native_password']
    volumes:
      - .docker/data/db:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    networks:
      - thibaultvlacich

  wordpress:
    depends_on:
      - database
    image: wordpress:latest
    volumes:
      - ./wordpress/wp-content:/var/www/html/wp-content
    environment:
      WORDPRESS_DB_HOST: database:3306
      WORDPRESS_DB_USER: ${MYSQL_USER}
      WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
      WORDPRESS_DB_NAME: ${MYSQL_DATABASE}
    labels:
      - traefik.enable=true
      - traefik.frontend.rule=Host:thibault.vlacich.fr;PathPrefix:/blog;PathPrefixStrip:/blog
      - traefik.port=80
      - traefik.docker.network=http_network
    networks:
      - thibaultvlacich
      - http_network

Everything is working fine, at the exception of the admin. When I go to https://thibault.vlacich.fr/blog/wp-admin, I’m redirected to https://thibault.vlacich.fr/wp-admin, and then can’t access the admin.

Any idea how I could fix that? (Both WP_HOME and WP_SITEURL are correctly set to https://thibault.vlacich.fr/blog)



Solution 1:[1]

I found this configuration in the docker-compose working. By just having everything under a subfolder in /var/www/html/SUBFOLDER_NAME it solves the issue of redirecting for /wp-admin.php page.

The following is the docker-compose part for wordpress with my SUBFOLDER_NAME as 'blog'.

### Wordpress ###########################################
wordpress:
  image: wordpress:latest
  restart: always
  environment:
    WORDPRESS_DB_HOST: mysql
    WORDPRESS_DB_USER: root
    WORDPRESS_DB_PASSWORD: root
    WORDPRESS_DB_NAME: test
    WORDPRESS_CONFIG_EXTRA: 
      |
      define ( 'WP_SITEURL', 'http://localhost/blog' );
      define ( 'WP_HOME', 'http://localhost/blog' );
  volumes:
    - ./wordpress/blog:/var/www/html/blog
  depends_on:
    - mysql
  networks:
    - frontend
    - backend
  working_dir: /var/www/html/blog
  labels:
    - traefik.enable=true
    - traefik.frontend.rule=Host:localhost;PathPrefix:/blog
    - traefik.port=80

Solution 2:[2]

Hi My solution for Traefik V2

version: '3'

services:
  db:
    image: mysql:8.0
    container_name: blog_db
    restart: always
    env_file: .env
    environment:
      - MYSQL_DATABASE=blog
    volumes:
      - dbdata:/var/lib/mysql
    command: '--default-authentication-plugin=mysql_native_password'
    networks:
      - app-network

  wordpress:
    depends_on:
      - db
    build: .
    container_name: wordpress
    restart: always
    environment:
      WORDPRESS_DB_HOST: blog_db:3306
      WORDPRESS_DB_USER: $MYSQL_USER
      WORDPRESS_DB_PASSWORD: $MYSQL_PASSWORD
      WORDPRESS_DB_NAME: blog

    volumes:
      - ./wordpress:/var/www/html/blog
    networks:
      - app-network
      - proxy
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=proxy"
      - "traefik.http.routers.blog.entrypoints=websecure"
      - "traefik.http.routers.blog.rule=(Host(`example.com`) && PathPrefix(`/blog{regex:$$|/.*}`))"
      - "traefik.http.routers.blog.priority=10"
      - "traefik.http.services.blog.loadbalancer.server.port=80"
      - "traefik.http.routers.blog.tls=true"
      - "traefik.http.routers.blog.tls.certresolver=cloudflare"



volumes:
  dbdata:

networks:
  app-network:
    driver: bridge
  proxy:
    external: true

Solution 3:[3]

A possible solution:

Use this to compose: Change SOME_LOCAL_IN_YOUR_SERVER for a path in your server and mkdir 2 folders inside (db and wpdata)

version: '3'
    
services:
       db:
         image: mysql:5.7
         volumes:
           - /SOME_LOCAL_IN_YOUR_SERVER/db:/var/lib/mysql
         networks:
           - wordpress
         environment:
           MYSQL_ROOT_PASSWORD: somewordpress
           MYSQL_DATABASE: wordpress
           MYSQL_USER: wordpress
           MYSQL_PASSWORD: wordpress
       wordpress:
         depends_on:
           - db
         image: wordpress:latest
         environment:
           WORDPRESS_DB_HOST: db:3306
           WORDPRESS_DB_USER: wordpress
           WORDPRESS_DB_PASSWORD: wordpress
         volumes:
            - /SOME_LOCAL_IN_YOUR_SERVER/wpdata:/var/www/html 
         networks:
            - traefik-public
            - wordpress
         labels:
                - 'traefik.enable=true'
                - 'traefik.docker.network=traefik-public'
                - 'traefik.http.routers.blog.rule=Host(`your.site.com`) && PathPrefix(`/blog`)'
                - 'traefik.http.routers.blog.entrypoints=websecure' 
           
    volumes:
        db_data:
        
    networks:
        traefik-public:
           external: true
        wordpress:

Compose to populate wp data in wpdata dir

Enter in the wpdata folder:

cd /SOME_LOCAL_IN_YOUR_SERVER/wpdata

edit wp-config.php and insert this 2 lines:

define('WP_HOME','your.site.com/blog');
define('WP_SITEURL','your.site.com/blog');

make blog dir in wpdata:

mkdir blog

copy all the content from wpdata to wpdata/blog:

cp * -R blog/

edit the index.php (in wpdata dir, NOT THE blog dir version)

require( dirname( __FILE__ ) . '/wp-blog-header.php' );
must be:
require( dirname( __FILE__ ) . '/blog/wp-blog-header.php' );

Based on: https://blog.sakuragawa.moe/containerized-wordpress-from-sub-directory/ and https://wordpress.org/support/article/giving-wordpress-its-own-directory/#method-ii-with-url-change

Solution 4:[4]

Alternatively, you can try this without creating a subfolder. Try to add the following line in wp-config.php

$_SERVER['REQUEST_URI'] = str_replace("/wp-admin/", "/blog/wp-admin/",  $_SERVER['REQUEST_URI']);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Masoud Tavakkoli
Solution 3 Vinicius Maran
Solution 4 Ishant Sitaula