Nginx rewrite makes me crazy - nginx

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.

Related

nginx how to redirect all pagination

I need help with my nginx configuration.
The goal is to have all requests to my site like site/page1 site/smth/page1 be redirected to just site/ and site/smth/ basically for all requests ending like page[number]
I tried some examples that I found like rewrite ^/page/(.*)$ /$1; still wasn't able to get the redirection. Maybe I misplaced it, not quite sure where I should put the sting. Tried location and server blocks.
The nginx documentation examples for redirecting were a bit too hard to understand for me, so a little explanation would be great.
If you need a 301 HTTP redirection, try this rewrite rule (before the first location block):
rewrite ^(.*/)page\d+$ $1 permanent;
You can try something like this (not tested)
location ~ ^/(.+)/page[0-9]+$ {
rewrite ^/(.+)/page[0-9]+$ /$1 last;
}

Nginx rewrite rule: Add subfolder to URI if not there

Hello I'm trying to write a Nginx rewrite rule that adds a subdirectory to my URL if it's missing. For example, I need http://example.com to be re-written (and redirected) to http://example.com/legacy-app. Can't seem to find the proper example to do this.
You can use a rewrite directive, but an exact match location block is most efficient:
location = / {
return 301 /legacy-app;
}
See this document for more.
We ended up using this:
rewrite ^/$ /legacy-app/ last;

Nginx - rewrite rule url encoding

Got a rewrite rule for nginx;
if ($query_string ~ "page=search&shift=1&reset=1&search=([^&]+)(&searchButton=Zoek)?"){
set $search $1;
rewrite ^/?$ https://$host/zoeken/$search/? permanent;
}
When the search query hi/hi is performed, the output of the URL is:
website.com/zoeken/hi/hi, which returns a 404.
The expected outcome is website.com/zoeken/hi%52hi/
What is the correct rewrite rule to have the expected outcome?
Seems that in nginx we can't turn off or on URL encoding.
We got a choice to install another module, http://wiki.nginx.org/HttpSetMiscModule#set_escape_uri
But since we want to avoid installing modules, we set the rewrites rule in apache, rather than nginx.
Too bad.

Rewrite a URL in nginx without redirecting

I'm trying to create a rewrite with nginx that will change the displayed URL without actually redirecting to it. EG: http://example.com/item/rest-of-path would become http://example.com/folder/rest-of-path. I've been working with different variations of this code with in my nginx.conf:
location ~ /example {
rewrite ^/example/(.*) /folder/$1 last;
}
but that doesn't seem to be doing the trick. Any ideas where I'm going wrong here? I'll admit I'm still pretty new to server-side rewrites in general.
Try this:
location ~ /example {
rewrite ^/example/(.*) /folder/$1 break;
}
use break.
Try this. It isn't necessary to place it under the location block, and don't forget to reload nginx.
rewrite ^/example/(.+)$ /folder/$1 last;

nginx rewrite question

I'm new to nginx and I got a rewrite problem here:
I want to permanently redirect http://domain1.com/abc.php to http://domain2.com,
but I want to keep http://domain1.com/abc.php?param=value, I've tried put
rewrite ^/abc\.php$ http://domain2.com last;
which works for http://domain1.com/abc.php, unfortunately it rewrites everything that starts with the '/abc.php', I'm really confused why this is happening, any ideas?
Thanks in advance.
Nginx rewrites generally don't "see" the query string as part of the URI, which is why your existing rewrite isn't working - to Nginx it's always ^/abc\.php$ whether there's a query string or not.
Instead, I'd try this (adapted from the documentation):
if ($args !~ param=value) {
rewrite ^/abc\.php$ http://domain2.com permanent;
}
But be aware that if is evil.

Resources