Is there a way to copy then exclude multiple directories from a url using regex for redirects?

21 views Asked by At

I've created an offsite bucket to store media files. I'm trying to redirect all of the media library (old) file URLs to the bucket (new) but need to exclude some sub-directories that do not exist on the bucket.

Is it possible to write a regex rule that copies all information after the domain and trailing slash (olddomain.com/), check it for the directories I would like to exclude, then append it after the trailing slash of the new domain?

Old

  • olddomain.com/files/

  • olddomain.com/files/sub1/

  • olddomain.com/files/sub2/ *(exclude from match)

  • olddomain.com/files/sub3/ *(exclude from match)

New

  • newdomain.com/files

  • newdomain.com/files/sub1/

Thanks in advance.

Tried...

Source

^/files/(?!sub2|sub3)(.*)

Target

newdomain.com/$1

1

There are 1 answers

0
Jan Skácel On

From the looks of it, you might have forgotten /files/ in target. Nginx setup could look like this:

server {
    server_name olddomain.com;
    location /files/ {
        rewrite ^/files/(?!sub2|sub3)(.*)$ https://newdomain.com/files/$1 redirect;
    }
}