2

I whould like this simple rewrite rule:

http://example.com/8743b52063cd84097a65d1633f5c74f5?param1=999&param2=2222

to be redirected to:

http://example.com/index.php?param1=999&param2=2222&hash=8743b52063cd84097a65d1633f5c74f5

The following is my default location:

location / {
    try_files $uri $uri/ /index.php$is_args$args;
}

How can I achieve this using Nginx rewrite?

1 Answer 1

2

Using a rewrite statement:

rewrite "^/(\w{32})$" /index.php?hash=$1 last;

Or, within a location block:

location ~ "^/(?<hash>\w{32})$" {
    rewrite ^ /index.php?hash=$hash last;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Richard!! That worked! What if I want to take the entire segment no matter how many characters have on it?
You probably want to set minimum and maximum limits, otherwise other URIs may start looking like a valid hash. But \w{16,32} would match an alphanumeric sequence of between 16 and 32 characters for example. See the quick reference here.
Ok, I understand Richard, that make sense. I'll take a look at the reference. Thanks for your support!

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.