admin管理员组

文章数量:1122846

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:

/

it automatically redirects to:

/

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

/

it automatically redirects to:

/

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 / to the main multisite path / 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;
    }

}

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;
    }

}
Share Improve this question edited Feb 17, 2020 at 18:02 Alex asked Feb 15, 2020 at 15:27 AlexAlex 631 gold badge1 silver badge10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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;
    }

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;
}
}

本文标签: permalinksWordpress Pages Redirecting to Root with Wordpress Multisite (SubDirectory)