braces and nginx rewrite rule - nginx

How do I use braces in nginx rewrite rules, e.g.
rewrite ^(.*)\.[0-9]{8,}\.(js|css|png|jpg|gif|bmp) $1.$2 last;
running
nginx -t
gives an error, saying the rewrite rule is not terminated by ';', how can I fix this?
Thanks.

looks quoting the regex works
rewrite "^(.*)\.[0-9]{8,}\.(js|css|png|jpg|gif|bmp)" $1.$2 last;

Related

Nginx rewrite - swap path in URL

Please help me with nginx:
url www.server.com/products/customer/books
should be www.server.com/customer/products/books
So i need to swap /products/ with /customer/
Yes, you can do this with a rewrite. Add the following to your server block:
rewrite ^/customer/products/(.*)$ /products/customer/$1 last;
Or, if you have multiple URLs (ie customer & business), you can do this:
rewrite ^/(customer|business)/products/(.*)$ /products/$1/$2 last;

Proper nginx rewrite rule

How do you do nginx rewrite rule to convert
example.com/?subtopic=forum&action=show_board&id=1
into this
example.com/forum/board/1
or this
example.com/?subtopic=characters&name=Eternal
into this
example.com/characters/Eternal
I've found and tried to play around with this code
location / {
rewrite ^/subtopic/(.*)$ /?subtopic=$1 last;
}
but it doesnt work (i'm really a newbie into rewriting)
You may try these rules:
rewrite /(.*)/(.*)/(\d+) /?subtopic=$1&action=show_$2&id=$3 last;
rewrite /(.*)/(.*) /?subtopic=$1&name=$2 last;

simplify a rewrite rule on nginx

I would like to simplify those rewrite rules, don't know if this is possible, here is what I did :
rewrite ^/en/m/(.*)/$ /index.php?lang=en&cat=$1&platform=mobile last;
rewrite ^/en/(.*)/$ /index.php?lang=en&cat=$1 last;
rewrite ^/m/(.*)/$ /index.php?cat=$1&platform=mobile last;
rewrite ^/(.*)/$ /index.php?cat=$1 last;
it works but the number of rewrite rules is quite big..
The parameter /m/ (for mobile) is optional, is there a way to simplify that ? Any idea ?
I finally pass a single parameter and I parse it using php, it works like a charm, thank you Alexey!:)

Nginx rewrite rules additional arguments

I'm breaking my head on Nginx rewrite rules while migrating from Apache to Nginx.
I had .htaccess rewrite rules that made /search/foo+bar/2&pricerange=20-50 to search.php?search=foo+bar&page=2&pricerange=20-50
and I could access all the arguments like you would expect with $_GET['search'], $_GET['page'] and $_GET['pricerange']
But now with Nginx I have issues with this appended arguments like pricerange. Whenever I visit /search/foo+bar/2&pricerange=20-50 it does not translate the pricerange argument.
And when I visit the same url without the page number the pricerange argument gets added to the search argument. But only when I'm using more than one word concatenated with +'s.
My current Nginx rewrite rules:
location / {
try_files $uri /index.php$is_args$args;
rewrite ^/search/(.*)/(.*)/$ /search?search=$1&page=$2 last;
rewrite ^/search/(.*)/(.*)/?$ /search?search=$1&page=$2 last;
rewrite ^/search/(.*)/$ /search?search=$1 last;
rewrite ^/search/(.*)/?$ /search?search=$1 last;
}
GET arguments begin after the '?'
/search/foo+bar/2&pricerange=20-50
You need to replace '&' with '?' or rewrite rule
rewrite ^/search/(.*)/([0-9]+)&pricerange=(.*)$ /search?search=$1&page=$2&pricerange=$3 last;

Regular expression not match in nginx rewrite

I am writing a rewrite rule for nginx but keep getting loop issue
How do I write the regex to only match ?page=home but stops at ?page=m-home
This is what I have right now, but doesn't really work
if ($args ~ page=[^m\-](.+)){
set $a $1;
rewrite ^(.+)$ /cms/index.php?page=m-$a permanent;
}

Resources