3

I am trying to rewrite the following URL via Nginx:

http://www.domain.com/script.php?title=LONGSTRING&desc=LONGSTRING&file=LONGSTRING&id=THREELETTERS

into something like this:

http://www.domain.com/script/LONGSTRING/LONGSTRING/LONGSTRING/LONGSTRING/THREELETTERS.html

All I have been able to find so far is how to include a single variable. I have five variables to pass through, each terminated by a "/".

0

1 Answer 1

6

you can access a script parameter name in nginx through the $arg_name variable

rewriting the url with the script parameters to an seo-friendly url then becomes a simple rewrite like so:

location /script/script.php {
  rewrite ^ /script/$arg_title/$arg_desc/$arg_file/$arg_id.html last;
}

the reverse, rewriting the seo-friendly url to the php script version would be:

location /script/ {
  rewrite "^/script/([a-zA-Z]+)/([a-zA-Z]+)/([a-zA-Z]+)/([a-zA-Z]{3})$" 
          /script.php?title=$1&desc=$2&file=$3&id=$4 last;
}

Basically you have regex captures (each round bracket pair is a capture) that you can then reference with the $1, $2, ... variables

Sign up to request clarification or add additional context in comments.

2 Comments

This looks like the opposite from the question. The from and to patterns are the wrong way round. It's param-based to path based.
oops, you're right I did it the wrong way around, added the other direction

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.