3

Consider a URL as shown below:

http://myDomain.tld/anAddress/myFile.pdf?X=zzz&Y=kkk

How can I have it as :

http://myDomain.tld/anAddress/myFile.pdf/zzz/kkk

I want to get rid of both ?X and &Y. I'd be glad if someone let me know what rule I should use.

MY SERVER BLOCK :

server {
    listen 80;
    server_name _;

    location  / {
        root /my/root/path;

        rewrite ^/(.*)pdf/(.*)/(.*)$ /$1?md5=$2&expires=$3 break;

        secure_link $arg_md5,$arg_expires;
        autoindex on;

        secure_link_md5 "$secure_link_expires$uri aWord";

        if ($secure_link = "") { return 403; }
        if ($secure_link = "0") { return 410; }
    }
}

Purpose : To be more precise, my URL is like:

http://myDomain.tld/anAddress/myFile.pdf?md5=So1Me2Ha3Sh4&expires=123456789

I want it to be like:

http://myDomain.tld/anAddress/myFile.pdf/So1Me2Ha3Sh4/123456789

In Apache, we can use:

RewriteRule ^(.*)pdf/(.*)/(.*)$ $1?md5=$2&expires=$3

When I tried to convert it to NGINX form, it became:

rewrite ^/(.*)pdf/(.*)/(.*)$ /$1?md5=$2&expires=$3 break;

But it didn't work.

Parsa Samet
  • 767
  • 1
  • 7
  • 20
  • There are a number of problems with your question. What happens to the `.pdf` text in the original URI? Why is there a `(.*)` in the rewritten URI instead of `expires=`? Where have you placed the `rewrite` rule in your configuration file (because the use of `break` vs `last` is context sensitive)? – Richard Smith Jan 09 '17 at 10:21
  • @RichardSmith sorry it was a mistake, it's `expires=` not `(.*)`. I used it in `location ~ / { ... }`, and I am not that familiar with rewrite syntax in nginx, so I may have done it wrong. – Parsa Samet Jan 09 '17 at 10:55
  • You appear to rewrite `myFile.pdf` to `myFile.` (with a trailing `.`). Is the destination URI processed in the same location block? – Richard Smith Jan 09 '17 at 10:59
  • @RichardSmith Yes, I only have one block set for this. I'd be glad if you can help me. Thanks in advance. – Parsa Samet Jan 09 '17 at 11:17
  • You may want to edit your question and add further details, such as your `server` block. You wish to rewrite the URI to `/anAddress/myFile.pdf?md5=So1Me2Ha3Sh4&expires=123456789` - is this a script? Should it have a different extension? – Richard Smith Jan 09 '17 at 11:30
  • No, `/anAddress` is a directory in my server root, `myFile` is a file I wanna access, `md5= So1Me2Ha3Sh4` is an exapmle of md5 hash which I got by using secure_link_md5, and `expires=123456789` is secure_link_expires. If you think my server block is needed, I can update my question, but I guess there is no problem with my question since I'm just looking for a rewrite rule since mine doesn't work and I have no idea how I should write this in NGINX. – Parsa Samet Jan 09 '17 at 11:42
  • @RichardSmith I updated my question, you can check my server block now. Thanks bro. – Parsa Samet Jan 09 '17 at 11:48
  • Does `myFile` include a `.pdf` extension or not? – Richard Smith Jan 09 '17 at 11:53
  • @RichardSmith yes it does. – Parsa Samet Jan 09 '17 at 12:02

1 Answers1

3

It seems to me that you are not capturing the last three characters of the filename (i.e. pdf) which means that they are missing from the rewritten URI.

You should try moving the ), so that the pdf is captured as part of $1:

rewrite ^/(.*pdf)/(.*)/(.*)$ /$1?md5=$2&expires=$3 break;

EDIT: In response to your comment, you can add a new location block which matches the URIs you wish to reject, for example:

location ~* \.pdf$ {
    return 403;
}

The above regular expression location block matches any URI that ends with .pdf (with or without a query string). See this document for details.

Richard Smith
  • 1,243
  • 1
  • 8
  • 13
  • 1
    Mate, thanks you so much! There is only one question left, it's working but I can still access `myFile.pdf` by using the old URL including `?md5=...&expires=...`, is there any way to return 403 when using that kind of URL instead of the new one? – Parsa Samet Jan 09 '17 at 12:21
  • 1
    I have updated the answer. – Richard Smith Jan 09 '17 at 13:54