1

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.

2 Answers 2

2

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:

server {
  server_name example.com;

  root /home/path/to/site;

  location = / {
    # Change /index.html to whatever your static filename is
    try_files /index.html =404;
  }

  location / {
    return 301 http://blog.example.com$request_uri;
  }
}
0
1

As long as the root of the two domains will point to different directories, you'll need two server directives - something like this:

server {
        # this is the static site
        server_name example.com;

        location = / {
                root /home/path/to/static/page;
        }

        location /foo {
                return 301 http://blog.example.com$request_uri;
        }
}

server {
        # this is the WP site
        server_name blog.example.com;

        location = / {
                root /home/path/to/new_blog;
        }

        .... some other WP redirects .....
}
2
  • Sure, I have settings for blog.example.com like these and block.example.com works perfect. The question is how to remove redirection from root of example.com? Jul 12, 2012 at 23:01
  • It is strange that the code above is not working.. The permanent redirect is sometimes tricky. Did you try from another browser? If it somehow had cached the 301 response...
    – Tisho
    Jul 12, 2012 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.