Nginx Rewrite Not rewriting the url - nginx

I have tried this for hours and can't seem to get it. I copied other examples and still can't get rewrite to work.
I need my url on nginx to look like http://myurl.com/main/login, http://myurl.com/somethigelse/home, est. Any Help Appreciated. I'm new to nginx, seems a lot faster.
My nginx rewrite looks like this:
rewrite ^/([^/]+)/([^/]+)$ /index.php?db=$1&action=$2 last;
rewrite ^/([^/]+)/([^/]+)/$ /index.php?db=$1&action=$2 last;

It works for me. Do you tell nginx to reload the configuration (do a sudo service nginx reload)? Otherwise, nginx will still be using the old config.
Note, that you can use one line by making the final slash optional using a question mark:
rewrite ^/([^/]+)/([^/]+)/?$ /index.php?db=$1&action=$2 last;
# ^

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 all trailing / to /index.html with proxy pass

Using nginx as a reverse proxy, I'd like to mimic the index directive with proxy_pass. Therefore I'd like nginx to query /index.html instead of /, /sub/index.html instead of /sub/.
What would be the best approach to do this ?
Not sure if it's relevant, but the proxied server does answer HTTP 200 on /, but I'd still like to rewrite it to /index.html.
As the / request leaks some information by listing the directory content, I'd also like to be sure that no one will be capable of accessing it (like doing something like /sub/..).
Thanksies
Just add :
rewrite (.*)/$ $1/index.html last;
rewrite (.*)/..$ $1/../index.html last;
Should works

Multiple Nginx rewrites are not working properly

This is a rewrite for processing GET requests as subdirectories
rewrite ([A-Za-z0-9-]+)/([A-Za-z0-9-_]+)$ /__api/$1.php?r=$2 last;
rewrite ([A-Za-z0-9-]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)$ /__api/$1.php?r=$2&o=$3 last;
hello-world/var1_test
to
/__api/hello-world.php?r=var1_test
-> Working properly
hello-world/var1_test/var2_test
to
/__api/hello-world.php?r=var1_test&o=var2_test
->
Returns 404
Log shows that this request is actually accessing to hello-world/var1_test/var2_test. In short, rewrites are not working.
But it seems first rewrite is working properly while second rewrite is not working.
Is there anything wrong on the code?
EDIT:switching the line will make the rewrite passage on the first line able to use. but not the second line
Solved by myself.
It was about the directory misconfiguration which I forgot the start and the end of the URI. by using ^ and $ in the regular expression

remove /profile/ from a link

On my site my users click on a profile and the link shows up as www.website.com/profile/username and I would like it to only be www.website.com/username - any suggestions on how I can do this
Note: my webserver is nginx so using rewrite rules is ok, but I would want to know how to write them in the config file
Use the following rewrite rule:
rewrite ^/profile/(.*)$ /$1 permanent;

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