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

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.

Related

301 redirect if filename end has different characters

The filenames that I want to redirect are like following:
/wp-content/uploads/2022/06/DIDUFKU-BLINGSY9-bbteew-hhh-2022-3-110.pdf
/wp-content/uploads/2022/06/DIDUFKU-BLINGSY9-bbteew-hhh-2022-1-510.pdf
/wp-content/uploads/2022/06/DIDUFKU-BLINGSY9-bbteew-hhh-2022-2-116.pdf
etc.
I want to 301 redirect all that contains the unchanged URL part /wp-content/uploads/2022/06/DIDUFKU-BLINGSY9-bbteew-hhh-2022- to a file: /wp-content/uploads/2022/06/DIDUFKU-BLINGSY9-bbteew-hhh-2022a.pdf
I tried this in htaccess without any success:
RewriteRule ^(.+/)DIDUFKU-BLINGSY9-bbteew-hhh-2022-(.*) /wp-content/uploads/2022/06/DIDUFKU-BLINGSY9-bbteew-hhh-2022a.pdf [R=301,L]
Those files are deleted, those were duplicate files I decided to redirect but not redirecting. I put that rule in htaccess file in WP directory
If those files are deleted and you put this rule at the end of the .htaccess file, after the WordPress code block then it won't do anything since the request will be routed through WordPress and mostly likely trigger a (WordPress generated) 404 Not Found response.
This rule needs to go near the top of the .htaccess file, before the WordPress code block. ie. Before the # BEGIN WordPress comment marker.
However, your rule can be improved (to avoid unnecessary repetition), for example:
RewriteRule ^(wp-content/uploads/2022/06/DIDUFKU-BLINGSY9-bbteew-hhh-2022)-[\d-]+\.pdf$ /$1a.pdf
The $1 backreference contains the captured group from the RewriteRule pattern, ie. the string wp-content/uploads/2022/06/DIDUFKU-BLINGSY9-bbteew-hhh-2022.
You should test first with a 302 (temporary) redirect to avoid potential caching issues.

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

sitemap URL without trailing slash 404 error

I have wordpress website that I installed google XML Plugin.
all urls with and without trailing slash works properly.
but for sitemap.xml without trailing slash browser return 404 error.
I modified permalinks, .htaccess ,... but it doesn't work.
https://pteceng.com/partners/deltav =>OK
https://pteceng.com/partners/deltav/ =>OK
https://pteceng.com/sitemap.xml/ =>OK
https://pteceng.com/sitemap.xml =>NOT OK

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.

.htaccess 301 redirects wordpress

I'm trying top redirect a standard Wordpress "folder" URL of a page to a new one on the same domain while keeping all subfolders, query strings etc. From domain/old-path/ to domain/new-path/.
Now I have this:
RedirectMatch 301 ^/old-path(.*) /new-path$1
It works, but domain/old-path redirects to domain/new-path and, this being WP, another redirect happens from domain/new-path to domain/new-path/.
I would like both domain/old-path and domain/old-path/ to redirect straight to domain/new-path/ with the ending /.
All this while, like I said, keeping all "subfolders", query strings etc.
...this being WP, another redirect happens from example.com/new-path to example.com/new-path/.
If /new-path is a physical directory then it's not WP that is triggering the redirect, it is Apache's mod_dir that is "fixing" the URL.
However, since you are using WordPress, it is preferable to use mod_rewrite (RewriteRule) to perform the redirect instead of a mod_alias RedirectMatch in order to avoid conflicts. Different modules execute at different times during the request and WP already uses mod_rewrite.
Try something like the following at the top of your .htaccess file before any existing WP directives:
RewriteRule ^dir/?(.*) /new-dir/$1 [R,L]
As you can see, although the slash is optional in the RewriteRule pattern, this is never captured by the regex and the slash is always present in the substitution.
Change the R (temporary) redirect to a R=301 only when you are sure it's working OK.

Resources