Redirect any and all traffic to a folder to another url

Posted December 20, 2017 12.1k views


I am on Ubuntu 14.04.5 running Nginx 1.12.1.

I have a wordpress site, I would like to do the following:

Site A:
Site B:

I would like to redirect any traffic to* to This includes all subrectories and files (pdf, mp3, etc.)

If the url they are accessing is pointing to or to, I want everything to be redirected to Site B.

I have tried the following, which redirects anyone accessing but not if they have the full url to a file:

location ^/(wp-content.) {
rewrite ^/(wp-content.
) permanent;

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

This should do the trick:

location ~ ^/wp-content/(.*) {
    return 301 $scheme://$1;

This would redirect to If you want everything in your wp-content folder to redirect to a single location on the new server you can use this:

location ~ ^/wp-content/(.*) {
    return 301 $scheme://$1;

Or if this is another server that has it’s own wp-content folder you can use this:

location ~ ^/wp-content/(.*) {
    return 301 $scheme://$request_uri;