Regular expression not match in nginx rewrite - nginx

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;
}

Related

Nginx rewrite makes me crazy

the URL is:
http://xxxxxx/index.php?route=product/category&path=565
and why the heck the rules below dont work?!?!
THIS - dont work at all:
if ($request_uri ~* "^(.*)product/category&path=565"){
rewrite ^(.*)$ /index.php?route=product/category&path=174&filter=sale? last;
}
THIS - aint work too! =(
if ($args ~* "^(.*)product/category&path=565"){
rewrite ^(.*)$ /index.php?route=product/category&path=174&podbor=m:feniks? last;
}
Both of your rewrite rules work correctly (for me). The problem maybe that your application requires external redirection rather than internal redirection in order to see the change.
Consider using the redirect modifier rather than the last modifier. Of course, this will mean that the client will also see the rewritten URL.
See this document for more.

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;

Nginx rewrite domain and URLs

I'm configuring nginx with multiple server names, and trying to set up the following rewrite rules
redirect / on old.domain.com to new.domain.com/specific_page.php
redirect old.domain.com/$1 to new.domain.com/$1
In my "server" configuration, I have already the first rewrite condition working, but I cannot find the way to write the second.
if ($host = 'old.domain.com' ) {
rewrite ^/ http://new.domain.com/my-specific/link/list/info.php permanent;
rewrite ^/(.*)$ http://old.domain.com/$request_uri? permanent;
}
Any ideas how to handle easily this scenario? (I realise this might be an unusual setup.)
Actually I managed to solve my problem :
if ($host = 'old.domain.com' ) {
rewrite ^/$ http://new.domain.com/my-specific/link/list/info permanent;
rewrite ^(.*)$ http://old.domain.com$request_uri? permanent;
}
the first rewrite rule ^/$ matches only http://old.domain.com/ and rewrites it to the requested URL
The second rewrite rule ^(.*)$ matches whatever is behind the http://old.domain.com/ and rewrites the domain only.

Nginx rewrite syntax

I've seen a bunch of ngnix rewrites that have syntax like this:
server {
server_name www.example.com;
rewrite ^(.*) http://example.com$1 permanent;
}
I don't understand the ^(.*) part. Does the ^ take everything after the TLD of the uri?
The ^ does indeed match at the beginning of the string. In the case of nginx's rewrite directive this means the beginning of the path component of the actual URI. Unfortunately nginx's documentation is slightly incorrect. Quoting from http://www.nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite :
If the specified regular expression matches a URI, the URI is changed as specified in the replacement string.
However, this is technically wrong. rewrite does not match the whole URI/URL but only its path component (which always starts with a / even if the user only enters e.g. http://www.example.com instead of http://www.example.com/). Therefore rewrite ^(.*) http://example.com$1 permanent; does not turn into http://example.comwww.example.com.
If I remember it correctly, the ^ just sets the Regex rule to match the start of the string.
The parentheses are used to extract that part with the $1-9 variables.
Another solution from the Nginx wiki. Link
server {
server_name www.example.com;
rewrite ^ http://example.com$request_uri? permanent;
}

braces and nginx rewrite rule

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;

Resources