0

we have a large php application running on apache now which I would like to migrate to nginx. I’ve got everything to work correctly except for a RewriteRule in a .htaccess file.

The web app allows the user to store files which can be publicly downloaded. The files on the server can be found in the web root under their account folder, which is their domain name without subdomain and domain extension. So for the domain subdomain1.domain2.com the accountname = domain2

/files/{account}

Some examples:

/files/domain1/
/files/domain2/
/files/domain3/

However we want to have urls like https://subdomain1.domain1.com/files/file.pdf instead of https://subdomain1.domain1.com/files/account/file.pdf

This is the RewriteRule which works on apache.

RewriteCond %{HTTP_HOST} [www\.|subdomain1\.|subdomain2\.]? 
(domain1|domain2|domain3).(com|nl|org)+
RewriteCond %{REQUEST_URI} !files/(domain1|domain2|domain3)+\/
RewriteRule files/(.*) files/%1/$1 [L]

However trying to port this to nginx gives me headaches. I’ve tried the available options here (https://www.nginx.com/blog/creating-nginx-rewrite-rules/) but nothing seems to work as I want it.

Anyone has an idea on how to do this?

3
  • Are you sure about the square brackets in the first RewriteCond? Jun 20, 2018 at 14:19
  • @GerardH.Pille yes, just double checked it. It was written years ago by someone who doesn't work here anymore so I can't check why it was done that way
    – dingenling
    Jun 20, 2018 at 14:39
  • You can still correct it, he or she meant to use plain brackets, "^(www\.|subdomain1\.|subdomain2\.)? (domain1|domain2|domain3).(com|nl|org)$" (without the double quotes). The plus at the end is also funny, accepting eg. pussy.domain1.comcomcom. The square brackets mean "any single character between them", but because of the following question mark: "or not". :-) Jun 20, 2018 at 14:56

1 Answer 1

0

I hope you know that nginx doesn't support .htaccess, so add this to your configuration:

server {
        listen 80 ; 
        server_name ~^(www|subdomain(1|2))\.(?<domain>domain(1|2|3))\.(com|nl|org)$;

        location ~ ^/files/domain(1|2|3)/ {
        }

        location / {
          rewrite /files/(.*)$ /files/$domain/$1 last;
        }       

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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