Tinker
Tinker

Reputation: 319

Nginx: Redirect a certain file path url to its new path url

We moved the assets to a new container or folder. From /documents/THE_FILE.pdf was moved to /assets/client_files/files/documents/THE_FILE.pdf noticed the path is now within /assets/cleint_files/files/ directory.

The problem is, we already have bunch of content that have links to file but using the old path. I just want to make the work simple, since there are like 2,000 instances of these throughout the site. I'm hopeful this could be done using nginx that it will redirect a certain url to its new url when it detected a link like this http://www.domain.com/documents/THE_FILE.pdf. As long as the url's first path is /documents/, it will be redirected to new path /assets/client_files/files/documents/

Thank you.

Upvotes: 3

Views: 26402

Answers (1)

Hammer
Hammer

Reputation: 1582

A Simple rewrite will do, add this to your server block.

rewrite ^(/documents/.*)$ /assets/client_files/files$1 permanent;

This will throw a 301 redirect ot requests with URI staring with /documents/ to the new path.

For more information about Nginx rewrite, check the docs here

UPDATE

Also you can do this inside a location like this

location /documents/ {
    rewrite ^(.*)$ /assets/client_files/files$1 permanent;
}

Upvotes: 9

Related Questions