simplify a rewrite rule on nginx - 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!:)

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.

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;

Rewrite path to $_GET

I want to rewrite this: domain.com/foobar to domain.com?p=foobar.
I don't need subdirectories or multiple variables for this.
I am using nginx, and most other answers where either not what I wanted, or apache.
How can I achieve this?
I was able to solve it using the following rule in the location / {} block:
rewrite ^(.*)$ /index.php?p=$1 last;

Nginx don't rewrite certain URL's

Right now I'm rewriting all http requests to Page.php with
rewrite ^ Page.php last;
But I want to allow to access file from /Ajax/ folder directly.
I tried adding (at the beginning)
rewrite /Ajax/SamplePage.php /Ajax/SamplePage.php last; (for example)
But still landed on Page.php
Is there any way to write something like
rewrite ^/Ajax/ - last;
?
You can try:
rewrite ^(?!(/Ajax/))(.*)$ /Page.php last;

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;

Resources