Proper nginx rewrite rule - nginx

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;

Related

Prestashop 1.7.8.6 multistore nginx rewrite rules

I install the prestashop 1.7 as a multistore and write nginx rewrite rule as
location /shop-1/ {
rewrite ^/shop-1/(.*)$ /$1 last;
try_files $uri $uri/ /index.php?$args;
}
location /shop-2/ {
rewrite ^/shop-2/(.*)$ /$1 last;
try_files $uri $uri/ /index.php?$args;
}
I follow the nginx conf file from https://devdocs.prestashop.com/1.7/basics/installation/nginx/
Now the Q is Parent domain navigating correctly and showing images but multistore redirected corrected to http://example.com/{shop-1 or shop-2} but not showing the images on the multishop urls, getting nginx 404 error on multishop url but same image showing on a parent domain.
example:
http://example.com/shop-1/45-medium_default/skirt.jpg not showing the image
http://example.com/45-medium_default/skirt.jpg showing the image
To explain what happened here I need to refer to nginx request processing phases, a subject not many people actually understand correctly.
Here is a set of rewrite rules being executed at the NGX_HTTP_SERVER_REWRITE_PHASE:
rewrite ^/(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$1$2.jpg last;
rewrite ^/(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$1$2$3.jpg last;
rewrite ^/(\d)(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$3/$1$2$3$4.jpg last;
rewrite ^/(\d)(\d)(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5.jpg last;
rewrite ^/(\d)(\d)(\d)(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6.jpg last;
rewrite ^/(\d)(\d)(\d)(\d)(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7.jpg last;
rewrite ^/(\d)(\d)(\d)(\d)(\d)(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8.jpg last;
rewrite ^/(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9.jpg last;
rewrite ^/c/([\w.-]+)/.+\.jpg$ /img/c/$1.jpg last;
As you can see, none of them matches the /shop-1/45-medium_default/skirt.jpg request URI. So on the next turn, during the NGX_HTTP_FIND_CONFIG_PHASE your location /shop-1/ { ... } will be selected to handle the request. Next, after rewrite ^/shop-1/(.*)$ /$1 last; rule being executed, your request URI will be rewritten to /45-medium_default/skirt.jpg, and due to the used last flag on the rewrite directive the NGX_HTTP_FIND_CONFIG_PHASE will be executed again. However the NGX_HTTP_SERVER_REWRITE_PHASE won't be executed again, and a new URI won't be rewritten according to those rules. What you should do instead is to place a rewrite rule at the server level before the ruleset for rewriting image requests:
rewrite ^/(?:shop-1|shop-2)(/.*) $1;
... image URIs rewrite rules here
Note that I don't use last (or break) flag for this rule since the rewrite rules chain should not be terminated after this rewrite will be triggered. None of those two locations that you show in your question will be needed at all.

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 odoo 8

I am having a few issues trying to rewrite some URLs in nginx.
I have a basic website made using Odoo's CMS. My goal is to make the URLs "pretty"
eg. example.com/services would be what the customer types in and sees in the URL bar but it loads example.com/pages/website.services
The rewrites I have are
location /services {
rewrite / /page/website.services last;
}
location /news {
rewrite / /blog last;
}
location /contact-us {
rewrite / /page/website.contactus last;
}
/contact-us and /news work as intended but /services is still showing example.com/page/services in the URL bar instead of example.com/services
Any help or pointers would be greatly appreciated.
Ok so I managed to solve my problem.
my rewrites that work are:
location /services {
rewrite / /page/services last;
}
location /news {
rewrite / /blog last;
}
location /contact-us {
rewrite / /page/contactus last;
}
I'm not entirely sure why but I think it may have something to do with Odoo doing its own redirects which is why I thought /page/website.services was an actual page.
It confused me somewhat because the /page/website.contactus was working. It may have been because the contactus is created with a module and the services was just created as a page.
If anyone else has a better explanation feel free to post.

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.

Resources