0

I have a route with urls that can have an optional extra field. It can be either of the form :

  • "/my-route/azezaezaeazeaze.123x456.jpg"
  • "/my-route/azezaezaeazeaze.123x456.6786786786.jpg"

with :

  • "azezaezaeazeaze" being a mongoId
  • 123x456 two integers separated by "x"
  • 6786786786 a unix timestamp
  • jpg an image extension (could be jpeg, png, gif...)
  • all those are separated by a "."

I would like to remove the optional part (the timestamp) from the request with the http rewrite module. So that the second url effectively becomes lie the first.

I made a small test on regex101 to get the groups, but : - it doesn't seem to be the right syntax for nginx - I do not see how it will allow me to remove the timestamp

How can I remove the timestamp from that url?

2 Answers 2

1

Starting from the right-hand end, you need to match a dot followed by anything except a dot, so we have (\.[^.]*)$, then moving to the left, we want to match a dot followed by only digits \.[0-9]*, which we dont want to capture, and then to the left of that we want everything. I ended up with something like this:

rewrite ^(.*)\.[0-9]*(\.[^.]*)$ $1$2 ;
Sign up to request clarification or add additional context in comments.

4 Comments

So it matches the url, but it is not rewriting it correctly. I am trying to figure out what the problem is.
sorry, \1\2 was the syntax for the substitution in your regex101 example. It should be $1$2 for nginx. As to the flags, that depends on the rest of your setup.
Ah you are correct it seems, I accepted your answer. Would you say that your regex is more efficient than mine (since it seems more strict with the digits) ?
it's not particularly efficient as .* ends up matching too much and causes backtracking until .[0-9] matches. You might try versions using several \.[^.]* as they will progress without backtracking.
0

Capitalizing on my first attempt and @meuh answer, I ended up with the following :

rewrite ^(/.*\..*)(\..*)(\..*)$ $1$3 last;

Now it works, but I would welcome any comment regarding the style/efficiency of this rewrite.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.