I have a site example.com that runs wordpress. Now I want to move this blog to subdomain blog.example.com, but also I want following:
example.com --> static page (not wordpress)
blog.example.com --> new address to the blog
blog.example.com/foo --> handled by wordpress
example.com/foo --> permanent redirect to blog.example.com/foo
So I tried this next config:
server {
server_name example.com;
location = / {
root /home/path/to/site;
}
location / {
rewrite ^(.+) http://blog.example.com$request_uri? permanent;
}
}
In this case redirection is working perfectly. Unfortunately example.com redirects to blog.example.com too.
The reason it’s redirecting is because when it tries to load the index file of example.com, it performs an internal redirect to /index.html, which is handled by your rewriting location. To avoid this, you can use try_files: