简体   繁体   中英

Currently, wordpress is served one example.com, but I want to serve it from example.com/blog with nginx

Using nginx - pretty straightforward setup and question, I have tried moving everything over to a blog directory under root, but wordpress 301s back to the example.com from example.com/blog. I assume I am missing something obvious or there is something inside wordpress causing this, but cannot figure it out

Here is my nginx block, which works, when serving from /.

server {
    listen       80 ;
    server_name  $SITE_URL;
    server_name _;
    # note that these lines are originally from the "location /" block
    root   /var/www/vhosts/$SITE_URL/httpdocs;
    index index.php index.html index.htm;
    location = /favicon.ico {
            log_not_found off;
            access_log off;
    }
    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }
    location / {
            # This is cool because no php is touched for static content.
            # include the "?" part so non-default permalinks doesn't break when using query string
        try_files \$uri \$uri/ /index.php?\$args;
    }
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)\$ {
            expires max;
            log_not_found off;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location = /50x.html {
        root /usr/share/nginx/html;
    }
    location ~ \.(php|phar)(/.*)?\$ {
        fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)\$; 
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_intercept_errors on;
        fastcgi_index  index.php;
        include        fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;
        fastcgi_param  PATH_INFO \$fastcgi_path_info;
    }
}

So my first attempt was to move everything up a directory into httpdocs/blog

and add

location /blog {
            # This is cool because no php is touched for static content.
            # include the "?" part so non-default permalinks doesn't break when using query string
        try_files \$uri \$uri/ /blog/index.php?\$args;
    }
    location ~ \.(php|phar)(/.*)?$ {
            fastcgi_split_path_info ^(/blog\.(?:php|phar))(/.*)$;
            fastcgi_pass unix:/run/php-fpm/www.sock;
            fastcgi_intercept_errors on;
            fastcgi_index  index.php;
            include        fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO $fastcgi_path_info;
        }

but no luck

Edit I have just noticed ip.com/bloggg will redirect to ip.com/gg so I think there is some rule somewhere but I have no clue.. I dont see it in nginx

Edit2

turns out there was a redirect plugin in the blog that rewrote /blog... whoops

Edit your Wordpress General Settings, change the both URLs as you need them.

"Site Address (URL): Enter the address you want people to type in their browser to reach your WordPress site. [...]"

Read carefully what you have to change.

https://codex.wordpress.org/Settings_General_Screen

您应该下载该wordpress的sql文件,然后用新的网址(例如“ http://example.com ”替换为“ http://example.com/blog ”)替换旧网址

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM