How to force Wordpress to remove trailing slashes?
-
I've searched around quite a bit for a solution here, but I can't find anything. I apologize if this is too technical for the forum.
I have a Wordpress site hosted on Nginx by WP Engine. Currently it resolves requests to URLs either with or without a trailing slash.
So, both of these URLs are functional:
<code>mysite.com/single-post</code>
and
<code>mysite.com/single-post/</code>
I would like to remove the trailing slash from all posts, forcing
mysite.com/single-post/
to redirect tomysite.com/single-post
. I created a redirect rule on the server:^/(.*)/$ -> /$1
and this worked well for end-users, but rendered the admin panel inaccessible. Somewhere, Wordpress is adding a trailing slash back on to the URL
mysite.com/wp-admin
, resulting in a redirect loop. I can't see anything obvious in .htaccess.Where is this rule adding a trailing slash to 'wp-admin' established?
Thanks very much
-
WPEis run on a base server using Apache with a Nginx proxy so you can use the WP engine 301 redirect system built-in or you can simply add a redirect to the HTAccess file. If you would like to use a tool to do this I recommend this one another alternative is to ask WP engine to make a change for you.https://www.aleydasolis.com/htaccess-redirects-generator/non-slash-vs-slash-urls/ApacheJust copy to your htaccess:```
https://example.com/page/**https://example.com/page** ``` <label for="nonslash">**Slash to Non-Slash URLs**</label> > <ifmodule mod_rewrite.c="">RewriteEngine on > RewriteCond %{REQUEST_FILENAME} !-d > RewriteRule ^(.*)/$ /$1 [L,R=301]</ifmodule> **Non-Slash to Slash URLs** ``` ****Apache** https://example.com/page**
> <ifmodule mod_rewrite.c="">RewriteEngine on > RewriteCond %{REQUEST_FILENAME} !-f > RewriteRule ^(.*[^/])$ /$1/ [L,R=301]</ifmodule>
USEING Nginx to do
**https://example.com/page/**
As you see, there is one tiny difference between those two URLs, and it’s the trailing slash at the end. In order to avoid duplicate content, if you are using Nginx you can **remove the trailing slash from Nginx** URLs. Place this inside your virtual host file in the server {} block configuration: > ``` > rewrite ^/(.*)/$ /$1 permanent; > ``` Full example: > ``` > server { > listen 80; > server_name www.mysite.com; > rewrite ^/(.*)/$ /$1 permanent; > } > ``` All done, now Nginx will remove all those trailing slashes.
USEING Nginx to do
https://example.com/page
https://example.com/page/Add a trailing slash by placing this inside your virtual host file in the server {} block configuration: > ``` > rewrite ^(.*[^/])$ $1/ permanent; > ``` Full example: > ``` > server { > listen 80; > server_name www.mysite.com; > rewrite ^(.*[^/])$ $1/ permanent; > } > ``` From now on, Nginx should add your trailing slash automatically to every url * https://www.scalescale.com/tips/nginx/add-trailing-slash-nginx/ * https://www.scalescale.com/tips/nginx/nginx-remove-trailing-slash/ I hope this helps, Tom
-
Hi John, sorry ive been on leave so not checked back on the forums.
Glad it looks like its working for you. I dont think the comments do anything except signify where word press has begun writing to the .htaccess (i dont run wordpress so can't be sure). Normally comments do nothing but signify something useful to the user.
I can try to breakdown the code a little for you, but my htaccess isn't fantastic so its by no means complete.
Firstline: RewriteCond%{REQUEST_FILENAME}!-d
RewriteCond% = This says use this condition if....
{REQUEST_FILENAME}!-d = ... is NOT a directoryRewriteRule^(.*)/$ /$1 [L,R=301]
I believe this bit takes a snapshot of the url upto the final / then rewrites it to that snapshot.The combination of these must mean it doesn't affect your wordpress admin directory. I know this code can break if your install is within a directory (as is discussed in the stackover flow link) but they have provided a solution for that in that topic. I would say test if on your live website to make complete sure it will work as this may be slightly different to your local install. Have a back-up ready just incase it doesn't.
Make sure you check every url including
Homepage
Pages
Posts
Category Pages
Sub Category Pages
Post Pages
Any images or filesTo make sure it is working as expected on all of them.
-
Thanks so much, ATP! It looks like writing the condition into .htaccess does the trick—at least for my local install. Is this because the commands located within the
BEGIN WordPress
END WordPress
comments only apply to URLs outside of the WP admin area?
Thanks again, ATP—that was a very thorough and helpful response!
-
Hi John
Did ATP's solution help you out? Let us know if we can look into this further!
-
Hi John,
I asked something similar myself something myself but im on the Magento platform. This should matter as the solution wasn't platform specific. It just involved editing htaccess file. If your up for editing your .htacccess file then it could be of some use. The topic URL is below and it contains multiple solutions for editing and removing the / and the debugging process we went through along the way. (Courtesy of Andy and Dirk) Hopefully its of some use to you
https://moz.com/community/q/cms-pages-multiple-urls
SUMMARY:
If you know how to edit your .htaccess and your ready to dive straight in this code should do it.RewriteCond%{REQUEST_FILENAME}!-d
RewriteRule^(.*)/$ /$1 [L,R=301]If you want the page with explanations and walk-through please see the original topic as editing your htaccess badly can cause all sorts of errors.
Edit: I realised i was probably a tiny bit lazy and should of probably included this link which is the original link i got sent from stackoverflow with instructions on how to to edit your .htaccess file.
http://stackoverflow.com/questions/21417263/htaccess-add-remove-trailing-slash-from-url
Dirks answer later in the post offer guidance on applying it to certain parameters which should prove helpful if your still having loop problems with the admin page.
-
Thanks for the replies, Donna, Martijn. I am running Yoast and considered adding the trailing slash, but:
-Most of the inbound links we have are to URLs with no slash
-The slash style seems a little dated in general-few sites use them these days.
I'd really love to just figure out how to solve the issue a little closer to the root.
-
Hi John,
It seems obvious, but why not go for a adding a trailing slash to every URL instead or removing it, would solve your issues at least.
-
Are you using the yoast SEO plugin? There is a setting under Advanced > Permalinks that forces a trailing slash onto URLs. I'd try looking at that first.
Got a burning SEO question?
Subscribe to Moz Pro to gain full access to Q&A, answer questions, and ask your own.
Browse Questions
Explore more categories
-
Moz Tools
Chat with the community about the Moz tools.
-
SEO Tactics
Discuss the SEO process with fellow marketers
-
Community
Discuss industry events, jobs, and news!
-
Digital Marketing
Chat about tactics outside of SEO
-
Research & Trends
Dive into research and trends in the search industry.
-
Support
Connect on product support and feature requests.
Related Questions
-
Why add .html to WordPress pages?
A site I may take over has a plugin that adds .html to the pages. I searched online but I’ve only found how to add it rather than why to add it. Is it needed? If I remove it, I’ll have to be careful with SEO / indexed pages and redirects. The site is running 3.x.x and 90% of the plugins have not been updated in over 5 years including this one. Before I update to 4.7.x, I am trying to understand the landscape (pros / cons) on why something could be used and if I need to find a suitable replacement for it.
Technical SEO | | acktivate2 -
How do I redirect the Author archive page in Wordpress?
If you do a search for my name on Google, the first result is the author archive page of my Wordpress blog. I would like to redirect the author page to my "about me" page but cannot add a 301 as the author page is created dynamically in Wordpress. Anyone know how I can do this?
Technical SEO | | richdan0 -
Wordpress and Redirects?
I want to update my permalinks - actually I want to change the URL's to fit the content and keywords better. I can choose "edit" the URL, but don't I need a redirect? I don't see any htaccess Plugin installed.......is that what I need to be able to change my URL's in Wordpress?
Technical SEO | | cschwartzel0 -
Double Slash // in URL
My client is using double forward slahes in URL like this "//" is this affecting SEO?
Technical SEO | | yanaiguana1110 -
Site (Subdomain) Removal from Webmaster Tools
We have two subdomains that have been verified in Google Webmaster Tools. These subdomains were used by 3rd parties which we no longer have an affiliation with (the subdomains no longer serve a purpose). We have been receiving an error message from Google: "Googlebot can't access your site. Over the last 24 hours, Googlebot encountered 1 errors while attempting to retrieve DNS information for your site. The overall error rate for DNS queries for your site is 100.00%". I originally investigated using Webmaster Tools' URL Removal Tool to remove the subdomain, but there are no indexed pages. Is this a case of simply 'deleting' the site from the Manage Site tab in the Webmaster Tools interface?
Technical SEO | | Cary_PCC0 -
ASP.net to Wordpress - Conserving the Backlink Profile
I am converting a site from ASP.net to wordpress. The ASP site has a decent backlink profile. Most links are pointing to their homepage. Is there anything I should be on the lookout so I do not lose their link profiles? I believe we are using the same server.
Technical SEO | | rmontanez0 -
The best managed hosting companies for WordPress
I'm looking for a reliable managed hosting company specializing in hosting WordPress sites with 24-hour phone support. What companies would you recommend?
Technical SEO | | translate0 -
Adding .html To Wordpress Site
I am working on a team (my part is the SEO) where the developer added the .html extension to the permalinks. I don't understand why, on Wordpress, you would do this. Is there any benefit, or penalty for it as an SEO standpoint? I usually just set mine up %postname% as the permalink structure, but I am not the web design lead on this project. I asked the designer why, but he seems to be reluctant to answer any of my emails about his work, (like he is above that or something). Not wanting to make things worse, I dropped it and thought I would ask here since I saw a few posts in reference to it today in the forum. Is there an advantage or disadvantage (either way) to using the .html extension on a Wordpress site?
Technical SEO | | kbloemendaal0