Benehmb
Benehmb

Reputation: 15

NGINX rewrite regex not working correctly

community, I have a Problem and I can't find a solution. I have a website running in which you can open things. You can use the path: https://localhost/open/index.htmp?id=1234 (or without the index.html in the URI). I want to open it throughout the path https://localhost/open/1234, so I set up a NGINX rewrite-rule:

rewrite "^/open/(?!\?id=.*)(.{1,})$" /open?id=$1 permanent; (I also tried last instead of permanent)

This rule, redirected me many times, so my path looked like /open/?id=&id=&id=&id=&id=1234 I also tried:

try_files $uri $uri/ /?id=$request_uri

None of them worked. What have I done wrong?

Upvotes: 1

Views: 661

Answers (1)

Richard Smith
Richard Smith

Reputation: 49672

You do not need and cannot use the (?!\?id=.*) part in the rewrite directive, as Nginx uses a normalised URI when matching location, try_files and rewrite directives.

You could focus the regular expression to only match numeric IDs, for example:

rewrite ^/open/(\d+)$ /open/?id=$1 permanent;

The regular expression operator + is the same as {1,}.

Upvotes: 1

Related Questions