使用WordPress多站点(子目录)重定向到根目录的WordPress页面

时间:2020-02-15 作者:Alex

我正在使用Wordpress Multisite(具有子目录结构)。一切都很好,而不是Wordpress页面。当我发布一个页面并尝试访问它时,它会重定向到主多站点。

例如,如果页面位于以下路径中:

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

它会自动重定向到:

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

或另一个示例,如果页面位于以下路径中:

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

它会自动重定向到:

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

我正在使用一个名为Custom Permalinks的插件来让帖子的URL看起来更好。我使用nginx作为web服务器。我正在开发这个网站,所以目前是在IP上,而不是在域上。我正在重定向当前根域https://example.com/ 到主多站点路径https://example.com/en-us/ 使用重定向插件。

这是我的nginx conf文件:

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

}

2 个回复
SO网友:AlanSon

您是否为具有子目录结构的多站点正确设置了wp配置?禁用自定义Permalink插件并在不使用它的情况下进行测试,是否有效?测试和调试时,请确保清除缓存并禁用所有缓存插件。在添加任何额外内容之前,请使用Wordpress默认主题,不要使用插件。是否重新启动了Nginx?

下面是一段视频,展示了我是如何进行设置的:https://youtu.be/0hZfEivLfFU

以下是我的Nginx配置示例:

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

SO网友:AlanSon

检查您的重写,确保它遵循以下内容

 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;
}
不是这个

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