Welcome! Log In Create A New Profile

Advanced

Nginx location group capture + rewrite?

Posted by Mayhem30 
Nginx location group capture + rewrite?
April 17, 2016 12:07PM
I'm trying to do a group capture in a Nginx location block and it's not working for me.

Is what I am trying to do even possible?

location ~* /(?<cat>cars|trucks|bikes|motorcycle|quads) {
rewrite ^/$cat/([0-9]+)(.*)$ /page.php?id=$1 last;
}

The error message I am receiving is :

"^/$cat/([0-9]+)(.*)$" does not match "/cars/120/new-car-rentals/"

I have a lot more categories than what I am posting, and trying to prevent writing a rewrite 5x for each specific category name.

This is where I got the syntax from : http://nginx.org/en/docs/http/server_names.html#regex_names

Any help would be appreciated.
Re: Nginx location group capture + rewrite?
April 17, 2016 10:49PM
I solved the issue by doing this instead :

location ~* /(cars|trucks|bikes|motorcycle|quad-bikes) {
rewrite ^/([a-zA-Z-]+)/([0-9]+)(.*)$ /page.php?id=$2 last;
...
...
}


The regex ([a-zA-Z-]+) allows me to use characters a-z (case insensitive) with possible dashes in my category / page names.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 302
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready