nginx how to redirect all pagination - nginx

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

Related

Redirecting old url to Nginx with param

I have an old URL structure like this archives.php?l=e and I moved it to a new structure archives/e
I'm trying to 301 redirect all the old urls, but I have run into troubles. This is what I'm currently doing but it's not working.
location ^~ /list.php {
rewrite /archives/$1/ permanent;
}
Any help would be greatly appreciated.
I figured it out and I have to say Nginx has some nice magic for this. Using arg_l solved it. Somehow it creates a variable for each param.
location ^~ /archives.php {
rewrite ^/archives.php$ /archives/$arg_l? permanent;
}

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 replace // with / in URL

We are currently getting web requests with www.domain.com//index.php which are resolving, but causing issues with google. How can we rewrite the request to catch these and redirect to www.domain.com/index.php
Thanks
By default ngingx merges double-slashes and urls like www.domain.com//index.php works well.
You can turn off merging and make rewrite rule with redirection:
merge_slashes off;
rewrite (.*)//(.*) $1/$2 permanent;

Nginx Rewrite Not rewriting the url

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;
# ^

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