Remove trailing slash for nginx WordPress multisite - wordpress

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

Related

How to redirect double slash in URL using NGINX

We have an external backlink with a double slash pointing to our website, ending on 404. Now I want to redirect that backlink to the correct version.
/en//effective-link-prospecting-with-marketing-miner-in-4-steps.html
to this version:
/en/blog/effective-link-prospecting-with-marketing-miner-in-4-steps.html
In NGINX I tried this (but it's not working I think because of double slash in the URL)
rewrite ^/en//effective-link-prospecting-with-marketing-miner-in-4-steps.html https://www.marketingminer.com/en/blog/effective-link-prospecting-with-marketing-miner-in-4-steps.html permanent;

How to improve this nginx rewrite rule

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!

How to write a .htaccess for URL redirect or rewrite for WordPress site?

I am trying to understand regex URL rewrites and redirects.
I need the following:
www.mydomain.com/tag/*/index.html (for example, mydomain.com/tag/winter-holidays/index.html or mydomain.com/tag/summer-holidays/index.html) to redirect to: www.mydomain.com/index.php/tag/WHATEVER/index.html
www.mydomain.com/*/ (for example, www.mydomain.com/four-roots-of-happy-living/, www.mydomain.com/four-roots-of-happy-living (without the ending slash), www.mydomain.com/living/, www.mydomain.com/living (without the ending slash) AND ALSO www.mydomain.com/category/happiness/ and www.mydomain.com/category/happiness (without the ending slash) to redirect to www.mydomain.com/index.php/WHATEVER/ or www.mydomain.com/index.php/WHATEVER/ WHATEVER/.
I have tried so many different combinations in my .htaccess file that my head is spinning now.
In my .htaccess (currently) I had this, but have deactivated it for the meantime until I can get this straight:
Redirect 301 /* /index.php/*
RedirectMatch 301 /*(.*)/? /index.php/$1
Note: My htaccess file was/is in my root directory, my WordPress site is in a wp folder off that, and it did not have a .htaccess file.

Redirect to a new url structure - same domain

So I recently moved my website from an old CMS to WordPress.
Re-imported 3000+ to WP and now all the old urls are 404.
I would like to 301 redirect all my posts within the htaccess.
The domain is still the same. I just need to remove the part ( category/category ) in the middle of all my urls.
This is the old url structure: (404s errors)
www.mywebsite.com/category-A/category-one/post-name
www.mywebsite.com/category-A/category-two/another-post-name
www.mywebsite.com/category-A/category-three/another-beautiful-post-name
etc.
This is the new structure I have (I need to redirect to this):
www.mywebsite.com/post-name
www.mywebsite.com/another-post-name
www.mywebsite.com/another-beautiful-post-name
etc.
Is there a way or do I need to thought each one by one?
Thank you
Assuming that those directory structures stay the same, you could use the following:
RedirectMatch 301 /category-A/category-one/(.*) /$1
RedirectMatch 301 /category-A/category-two/(.*) /$1
RedirectMatch 301 /category-A/category-three/(.*) /$1
This is a straightforward redirect. The 1st bit just looks for the relevant directory name(s) and then grabs the file name off the end.
RedirectMatch 301 /category-A/category-three/(.*)
Then it redirects to just the file name without the directories.
/$1
Note: You may need to try removing the trailing slashes &/or leading slashes to get the exact result you need. But, it should essentially be what you need.

Wordpress says page "not found" if includes trailing slash in URL?

For every page on this subdomain, if there is a trailing slash it always says page not found, the page only loads correctly without the slash at the end,
eg it doesn't work : abc.com/us/ + redirects to abc.com/us/:
it does work abc.com/us
Every other subdomain in the multisite works correctly
In your .htaccess file,
Add the following .htaccess code to fix the trailing slash issue.
RedirectMatch 301 ^(.+)/$ $1
The above code will remove trailing slash (/) from the URL except the root.

Resources