Let's say we can a blog hosted as Wordpress.com but visible on our custom domain in subpath due to SEO. WordPress.com supports changing domain/subdomain but not path.
We could either make our own installation, change hosting provier or make nginx proxy with URL rewrites.
- WP blog: https://example.wordpress.com/
- Desired site: http://example.com/blog/
This tutorial is inpsired by http://marsbard.github.io/2016-07-30-replace-urls-behind-nginx-reverse-proxy/.
Nginx needs the ngx_http_sub_module.
You can check with: nginx -V
.
nginx.conf
- put into the server
section and restart:
# nginx rule to make a wordpress.com site visible at subpath of our domain.
# Wordpress.com allows setting domain/subdomain, but not custom path.
# We have to proxy requests and rewrite base URLs rendered by WP to our site.
# Original base URL: https://example.wordpress.com/
# User facing base URL: https://example.com/blog/
# For administration better use the original domain.
# path on our site (visible to users)
location /blog/ {
# request get proxied to this site (hosted by wordpress.com)
proxy_pass https://example.wordpress.com/;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
# disable compression to make substitution work
proxy_set_header Accept-Encoding "";
# substitute URL rendered by wordpress.com to our location
# http://marsbard.github.io/2016-07-30-replace-urls-behind-nginx-reverse-proxy/
# plain URL:
sub_filter 'https://example.wordpress.com' '$scheme://$host/blog';
# escaped in JS:
sub_filter 'https:\/\/example.wordpress.com' '$scheme:\/\/$host\/blog';
# redirect in JS in URL parameters:
sub_filter 'redirect_to=https%3A%2F%2Fexample.wordpress.com%2F' 'redirect_to=$scheme%3A%2F%2F$host%2Fblog%2F';
# replace many times
sub_filter_once off;
# keep the last modified header
sub_filter_last_modified on;
}
Various places where base URL appears in plain or escaped forms:
https://example.wordpress.com
https:\/\/example.wordpress.com
https:\/\/example.wordpress.com\/wp-login.php?redirect_to=https%3A%2F%2Fexample.wordpress.com%2F
In order to get the Wordpress.com JetPack plugin to work with the proxy setup I needed to add the following:
Thanks!