wordpress wp-admin redirects to root directory and pure domain

  apache, docker, nginx, php, wordpress

Main problem

wordpress example.com/en/wp-admin/ redirects to example.com/wp-admin/ and the /en/ prefix is missed.

Description

Hello.
I have a main server at example.com . I wanted to serve a wordpress site at example.com/en
but the wordpress on separate server. The second server address is en.example.com

So I made a wordpress site using docker in second server using the docker-compose file i noted at the end and set the main server nginx to proxy_pass to en.example.com

server {
    server_name  example.com;
    ...
    location ^~ /en/ {
        include proxy_params;
        proxy_pass https://en.example.com/;
    }
    ...
}

and in the second server used this config for nginx:

server {
    server_name  en.example.com;
    location / {
        include proxy_params;
        proxy_pass http://127.0.0.1:4080; # server port 4080 is maped to container port 80
    }
}

after this doing these i faced forbidden error because of installing wordpress in sub directory. so i changed the apache2 that was running in the container config file from "/var/www/html" to "/var/www/html/en" to looking for index.php for both http and https.

Also changed .htaccess to allow accessing the wordpress in a way i wrote in below.
.htaccess (added en/ in line 3 and 7):

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /en/
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /en/index.php [L]
</IfModule>
# END WordPress

After doing These i could install wordpress and access that from example.com/en/ and all the links are fine and correctly use example.com/en/ .
But there is a big problem.

Main problem

Any time i wanna access admin page it redirects to root domain and ignores the subdirectory.
That means example.com/en/wp-admin/ redirects to example.com/wp-admin/ and the /en/ prefix is missed.
I appreciate for any help.

…………………………………………………………………………………………………………………………………………………………….

docker-compose.yml file that includes WP_HOME and WP_SITEURL correctly and some other extra configs i found on internet:

version: '3'


services:
  db:
    image: mysql:8.0
    container_name: en_db
    restart: unless-stopped
    env_file: .env
    environment:
      - MYSQL_DATABASE=enwordpress
    volumes:
      - en_db:/var/lib/mysql
    command: '--default-authentication-plugin=mysql_native_password'
    networks:
      - en_network

  wordpress:
    depends_on:
      - db
    image: wordpress:5.5-apache
    container_name: en_wordpress
    restart: unless-stopped
    env_file: .env
    ports:
      - 4080:80
    environment:
      - WORDPRESS_DB_HOST=db:3306
      - WORDPRESS_DB_USER=$MYSQL_USER
      - WORDPRESS_DB_PASSWORD=$MYSQL_PASSWORD
      - WORDPRESS_DB_NAME=enwordpress
      - WORDPRESS_CONFIG_EXTRA=
          define('WP_SITEURL','https://example.com/en/');
          define('WP_HOME','https://example.com/en/');
          define('WP_DEBUG', true );
          define('RELOCATE', true );
    working_dir: /var/www/html/en/
    volumes:
      - en_wordpress:/var/www/html/en
    networks:
      - en_network

volumes:
  en_wordpress:
  en_db:

networks:
  en_network:
    driver: bridge
 

Source: Ask PHP

LEAVE A COMMENT