How to improve this nginx rewrite rule - wordpress

I use this code to redirect a wordpress site from trailing slash vertion to non trailing slash verstion. For example from abc.com/en/ to redirect to abc.com/en
if (!-d $request_filename) { rewrite ^/(.*)/$ /$1 permanent; }
However, this code causes an unnecessary redirect to http first. So if I enter https://www.example.com/en/, it will first 301 to http://www.example.com/en, then 301 to https https://www.example.com/en. So how to improve this code to let it redirect directly to https version? And according to Nginx Pitfalls, using "if" is not recommended, can anyone please help to improve this code with a better version?
Thank you!

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

Remove trailing slash for nginx WordPress multisite

I have installed WordPress multisite in sub-directory abc.com/en/. I want to remove the trailing slash for SEO purposes. So pages like abc.com/en/xxx/ will be 301 redirected to abc.com/en/xxx without a trailing slash.
After checking many resources (I am using Nginx), I found the code below to be working for all pages except the multisite homepage abc.com/en/. WordPress always 301 redirects abc.com/en to abc.com/en/ with a trailing slash, so it will cause a redirect loop.
if (!-d $request_filename) {
rewrite ^/(.*)/$ /$1 permanent;
}
So how can I remove the trailing slash for the en/ too? Many thanks in advance!
add this line to functions.php
This this line you can fix redirect from domain.com/page to domain.com/page/ with trail slash
remove_filter('template_redirect', 'redirect_canonical');
Please don't forget clear cache or test on clean browser

How to avoid "too many redirects" with NGINX

Here is the rewrite I added and when I visit /name I have an infinite loop of redirects.
rewrite ^/name /name-and-more permanent;
What am I missing?
You rewrite ^/name to /name-and-more which results in a 301 redirect.
Then next request is for /name-and-more which matches the same rewrite ^/name, so it redirects again to /name-and-more, and again etc..
You have missed the trailing $ so that it matches exact URL /name.
So:
rewrite ^/name$ /name-and-more permanent;

Nginx rewrite simple issue

I've migrated my site and now got a huge list of links that I need to redirect to their corresponding new urls. I'm not a developer or sys admin and can't really help myself with the Nginx manuals.
A short snippet of my list looks like this:
rewrite /?tn=productview&c=34223&pid=8965214 /url1 permanent;
rewrite /?tn=productview&c=54424&pid=6180497 /url2 permanent;
rewrite /?tn=productview&c=54426&pid=2563446 /url3 permanent;
rewrite /?tn=productview&c=54426&pid=6889674 /url4 permanent;
The problem is that Nginx will in that particular case redirect the first line correctly and all following lines to "url1".
How should I write this so that each of this links be redirected to their corresponding new url?

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;

Resources