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
-
Toxic Link Removal
Greetings Moz Community: Recently I received an site audit from a MOZ certified SEO firm. The audit concluded that technically the site did not have major problems (unique content, good architecture). But the audit identified a high number of toxic links. Out of 1,300 links approximately 40% were classified as suspicious, 55% as toxic and 5% as healthy. After identifying the specific toxic links, the SEO firm wants to make a Google disavow request, then manually request that the links be removed, and then make final disavow request of Google for the removal of remaining bad links. They believe that they can get about 60% of the bad links removed. Only after the removal process is complete do they think it would be appropriate to start building new links. Is there a risk that this strategy will result in a drop of traffic with so many links removed (even if they are bad)? For me (and I am a novice) it would seem more prudent to build links at the same time that toxic links are being removed. According to the SEO firm, the value of the new links in the eyes of Google would be reduced if there were many toxic links to the site; that this approach would be a waste of resources. While I want to move forward efficiently I absolutely want to avoid a risk of a drop of traffic. I might add that I have not received any messages from Google regarding bad links. But my firm did engage in link building in several instances and our traffic did drop after the Penguin update of April 2012. Also, is there value in having a professional SEO firm remove the links and build new ones? Or is this something I can do on my own? I like the idea of having a pro take care of this, but the costs (Audit, coding, design, content strategy, local SEO, link removal, link building, copywriting) are really adding up. Any thoughts??? THANKS,
Technical SEO | | Kingalan1
Alan0 -
Hashtag in url seems to remove the google plus one
My site has a catalogue page (catalog in US) with #anchors so that customers can get straight to them. I even link from other pages to the #anchor on the catalogue page. so I have for example: www.example.co.uk/catalogue.htm www.example.co.uk/catalogue.htm#blueitems www.example.co.uk/catalogue.htm#redtems I understand google doesn't index after the #, here is the post I found: http://moz.com/community/q/hashtag-anchor-text-within-content#reply_91192 So I shouldn't have an seo problem. BUT, if I navigate to www.example.co.uk/catalogue.htm and plusone the page it will show the plusone and then I navigate to www.example.co.uk/catalogue.htm#blueitems the plus one is gone. The same happens in reverse, if i plusone www.example.co.uk/catalogue.htm#redtems, then that plusone doesn't show in www.example.co.uk/catalogue.htm. I added rel=canonical and that fixed the plusone problem, now if you plus one /catalogue.htm#redtems it still shows on catalogue.htm This seems a bit extreme and did I do the right things??
Technical SEO | | Peter24680 -
Removal of all low PR links
I have a lot of old directory links which where done years ago, most I think will be effecting my site. Is there away to find them all ie through open explorer and then remove them in one go?
Technical SEO | | Cocoonfxmedia0 -
Remove Bad Links Or Build New
Hello, After deeply assessing our back links we have come to the conclusion that we have too many links that have been devalued and also some spammy looking links.... Our next question is do we remove these bad links and start a fresh or do we just build new white hat links?? Thanks, Scott
Technical SEO | | ScottBaxterWW0 -
Wordpress Canonical Problem
I'm using wordpress for my website but m unable to implement Canonical tag property for pages under the same category, Like for matt's blog: The Tag is same .. for all pages under that category: http://www.mattcutts.com/blog/type/googleseo/ & http://www.mattcutts.com/blog/type/googleseo/page/2/ is it some hack or some plugin ? please suggest! thanks
Technical SEO | | AnkitRawat0 -
Web config redirects not working where a trailing slash is involved
I'm having real trouble with getting working redirects in place to use on a site we're re-launching with a modified url structure. Old URL: http://www.example.com/example_folder/ New URL: http://www.example.com/example-of-new-folder/ Now, where the old URL's have a trailing slash the web.config simply will not accept it. It says the URL can start with a slash, but not end with a slash. However, many of my URL's do end with a slash so I need a workaround. These are the rules I'm putting in place: <location path="example_folder/"></location> Thanks
Technical SEO | | AndrewAkesson0 -
Removing pages from website
Hello all, I am fairly new to the SEOmoz community. But i am working for a company which organizes exhibitons, events and training in Holland. A lot of these events are only given ones ore twice and then we do not organise them any more because they are no longer relevant. Every event has its own few webpages which provide information about the event and are being indexed by Google. In the past we did not remove any of these events. I was looking in the CMS and saw a lot of events of 2008 and older which are being indexed. To clean the website and the CMS i am thinking of removing these pages of old events. The risk is that these pages have some links to them and are getting some traffic, so if i remove them there is a risk of losing traffic and rankings. What would be the wise thing to do? Make a folder with archive or something? Regards, Ruud
Technical SEO | | RuudHeijnen0 -
WordPress Pretty Permalinks vs Site Speed
A couple of issues at play here as I wrestle with the best permalink structure for a site I'm toying with now. 1. I know that WordPress wants a unique number in the post to improve performance and db calls. 2. I know that for basic on-page SEO, most of us would opt for CATEGORY/POST or maybe even just post. I constantly change those. It's a bad habit, but sometimes you want the killer headline and a decent title in the post. So here is the issue: I can rewrite or use a plugin (anyone have a favorite) the permalinks to speed up site performance. We all know Google wants that. Maybe the permalink becomes /1234-foo But you know, a number in front of the URL just isn't awfully user friendly. If someone wants to read the foo post, it's nice to send them directly there. So would you trade off a slowdown in site speed for the prettiest permalinks for usability and SEO? And since you're asking a WP question, has anyone heard of a hard cap on static pages where the database starts dragging? The site I have in mind has 400 each posts and pages. Would moving platforms to Drupal or Joomla allow handling that many pages more effectively? Thanks for contributing and any help you can give. George
Technical SEO | | georgebounacos0