1

I am working with Wordpress Multisite (with sub-directory structure). Everything works fine instead of Wordpress pages. When I publish a page and I try to visit it, it redirects to the main multisite.

So for example, if the page is in this path:

https://example.com/en-us/my-new-page/

it automatically redirects to:

https://example.com/en-us/

Or another example, if the page is in this path:

https://example.com/es-es/my-really-new-page/

it automatically redirects to:

https://example.com/es-es/

I am using a plugin called Custom Permalinks to make posts URLs look nicer. I am using nginx as web server. I am developing the site, so currently on an IP, not a domain. I am redirecting the current root domain https://example.com/ to the main multisite path https://example.com/en-us/ with Redirection plugin.

This is my nginx conf file:

upstream php-handler-http {
    server 127.0.0.1:9000;
    #server unix:/var/run/php5-fpm.sock;
}

map $http_host $blogid {
    default 0;
    include /var/www/html/wp-content/uploads/nginx-helper/map.conf;
}

server {
    listen 80 default_server;
    server_name 123.123.123.123;

    root /var/www/html/;
    index index.php;

    access_log /var/log/nginx/wordpress_http_access.log combined;
    error_log /var/log/nginx/wordpress_http_error.log;

    # set max upload size
    client_max_body_size 2G;
    fastcgi_buffers 64 4K;

    if (!-e $request_filename) {
        rewrite /wp-admin$ $scheme://$host$request_uri/ permanent;
        rewrite ^(/[^/]+)?(/wp-.*) $2 last;
        rewrite ^(/[^/]+)?(/.*\.php) $2 last;
    }

    location / {
        try_files $uri $uri/ /index.php?$args ;
    }

    location ~ \.php(?:$|/) {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass php-handler-http;
        fastcgi_read_timeout 600;
    }

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location ~* \.(htaccess|htpasswd) {
        deny all;
    }

    # set long EXPIRES header on static assets
    location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
        expires 24h;
        access_log off;
    }

}
0

Check your rewrites to make sure it follow this

 if (!-e $request_filename) {
    rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
    rewrite ^/[_0-9a-zA-Z-]+.*(/wp-admin/.*\.php)$ $1 last;
    rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
}

not this

  if (!-e $request_filename) {
        rewrite /wp-admin$ $scheme://$host$request_uri/ permanent;
        rewrite ^(/[^/]+)?(/wp-.*) $2 last;
        rewrite ^(/[^/]+)?(/.*\.php) $2 last;
    }
0

Have you set the wp-config correctly for multisite with sub directory structure? Disable the Custom Permalink plugin and test without it, does it work? Make sure you clear your cache and disable all caching plugins when testing and debugging. Get it working with Wordpress default theme and no plugins before you add any extra stuff. Have you restarted Nginx?

Here's a video showing how I have it setup: https://youtu.be/0hZfEivLfFU

Here's an example of my Nginx config:

##################################
# WORDPRESS NGINX CONFIGURATIONS
##################################

server {
listen 80;
root /var/www/html;
server_name example.com;
access_log /var/log/nginx/wp_client_access.log;
error_log /var/log/nginx/wp_client_error.log;

location / {
    index                               index.php index.html;
    try_files                           $uri $uri/ /index.php?$args;
}

# Specify a charset
        charset                         utf-8;
# GZIP
        gzip                            off;

# Add trailing slash to */wp-admin requests.
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;

# Prevents hidden files (beginning with a period) from being served
location ~ /\. {
        access_log                      off;
        log_not_found                   off;
        deny                            all;
}




############
# Pass uploaded files to wp-includes/ms-files.php.
############

#       rewrite                         /files/$ /index.php last;

if ($uri !~ wp-content/plugins) {
        rewrite /files/(.+)$ /wp-includes/ms-files.php?file=$1 last;
}

# Rewrite multisite in a subdirectory '.../wp-.*' and '.../*.php'.
 if (!-e $request_filename) {
    rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
    rewrite ^/[_0-9a-zA-Z-]+.*(/wp-admin/.*\.php)$ $1 last;
    rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
}

# Rewrite multisite '.../wp-.*' and '.../*.php'.
#if (!-e $request_filename) {
#    rewrite /wp-admin$ $scheme://$host$uri/ permanent;
#    rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) /wp$1 last;
#    rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ /wp$1 last;
#}






###########
# SEND EXPIRES HEADERS AND TURN OFF 404 LOGGING
###########

        location ~* ^.+.(xml|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
        access_log                      off;
        log_not_found                   off;
        expires                         max;
}

# Pass all .php files onto a php-fpm or php-cgi server
location ~ \.php$ {
        try_files                       $uri =404;
        include                         /etc/nginx/fastcgi_params;
        fastcgi_read_timeout            3600s;
        fastcgi_buffer_size             128k;
        fastcgi_buffers                 4 128k;
        fastcgi_param                   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass                    unix:/run/php/php7.2-fpm.sock;
        #fastcgi_pass                   unix:/run/php/php7.0-fpm.sock;
        fastcgi_index                   index.php;
}

# ROBOTS

         location = /robots.txt {
               allow all;
               log_not_found off;
               access_log off;
        }

# RESTRICTIONS
location ~* /(?:uploads|files)/.*\.php$ {
 deny all;
}
}
  • Yes, the wp-config.php is set up correctly. Disabling the Custom Permalink plugin does not fix the issue. There is no cache plugin installed. Nginx restart several times, after updating nginx conf file. – Alex Feb 18 '20 at 1:41
  • One thing I have noticed in the URLs that redirect to the homepage of the multisite is this parameter in the response header: X-Redirect-By: WordPress - I think there might be some kind of conflict between nginx and Wordpress. – Alex Feb 18 '20 at 1:42
  • Yes that would make sense, according to Wordpress.org this is most likely due to a redirection plugin. Do you have any redirection plugins, maintenance or login redirection enabled? – AlanSon Feb 18 '20 at 6:47
  • Custom Permalinks is the only plugin I have installed that handles permalinks. Disabling it does not make any change, apart from that posts URLs stop to work, but pages URLs still do not work. – Alex Feb 18 '20 at 10:38
  • 1
    I managed to figured out the problem. The problem was with one custom rewrite rule I had when creating custom post types. This was messing up with URLs. I deactivated that and everything started to work. – Alex Feb 20 '20 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.