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
-
Trailing slash on the main website - do i need a 301 ? Is my 301 correct?
Hello, Im a bit confused. If i use a tool like majestic to look at my website links, www.example.com and www.example.com**/ have huge difference in their authority.** Do i need to make a 301 redirect to the site with the splash or not? Will google itself understand that they are my main site? Is this the "http://www.website.com.com/"/> correct canonical? Meaning it has trailing splash and also RewriteCond %{HTTP_HOST} ^www.example.com [NC]
Technical SEO | | advertisingcloud
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301] - this one has trailing splash, correct?0 -
Removing a canonical tag from Pagination pages
Hello, Currently on our site we have the rel=prev/next markup for pagination along with a self pointing canonical via the Yoast Plugin. However, on page 2 of our paginated series, (there's only 2 pages currently), the canonical points to page one, rather than page 2. My understanding is that if you use a canonical on paginated pages it should point to a viewall page as opposed to page one. I also believe that you don't need to use both a canonical and the rel=prev/next markup, one or the other will do. As we use the markup I wanted to get rid of the canonical, would this be correct? For those who use the Yoast Plugin have you managed to get that to work? Thanks!
Technical SEO | | jessicarcf0 -
Disavow everything or manually remove bad links?
Our site is likely suffering an algorithmic penalty from a high concentration of non-branded anchor text that I am painstakingly cleaning up currently. Incremental clean-ups don't seem to be doing much. Google recommends I 'take a machete to them' and basically remove or disavow as much as possible, which I am now seriously considering as an option. What do you guys recommend, should torch the earth (disavow all links with that anchor text) or keep it on life support (slowly and manually identify each bad link)?
Technical SEO | | Syed_Raza0 -
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 -
Can anyone recommend a wordpress forum for commercial theme help
Hi, i am trying to find a forum for wordpress when you have bought a commercial theme. I am looking at learning how to change a site for a better user experience and for better seo but there does not seem to be any wordpress forums that i can find for when people have bought a commercial theme any help in finding one would be great
Technical SEO | | ClaireH-1848860 -
Issues with trailing slash url
Recently, we have changed our website to www.example.com/super-rentals/ (example) and we have done a 301 redirection to the new urls from the old one. We have noticed in Google webmaster tool that urls without trailing slash as 404 error. www.example.com/super-rentals. Please let us know how to fix this issue as soon as possible. Note: Our previous urls are not the urls without trailing slash. It is a different url (www.example.com/super-rentals.htm) we have rewritten in to www.example.com/super-rentals/ only. I would like to know why GWT pulls out the urls without trailing slash and shows in 404 error. Thanks for your time
Technical SEO | | massimobrogi0 -
Generating a Sitemap for websites linked to a wordpress blog
Greetings, I'm looking for a way to generate a sitemap that will include static pages on my home directory, as well as my wordpress blog. The site that I'm trying to build this for is in a temporary folder, and can be accessed at http://www.lifewaves.com/Website 3.0 I plan on moving the contents of this folder to the root directory for lifewaves.com whenever we are go for launch. What I'm wondering is, is there a way to build a sitemap or sitemap index that will point to the static pages of my site, as well as the wordpress blog while taking advantage of the built in wordpress hierarchy? If so, what's an easy way to do this. I have generated a sitemap using Yoast, but I can't seem to find any xml files within the wordpress folder. Within the plugin is a button that I can click to access the sitemap index, but it just brings me to the homepage of my blog. Can I build a sitemap index that points to a sitemap for the static pages as well as the sitemap generated by yoast? Thank you in advance for your help!! P.S. I'm kind of a noob.
Technical SEO | | WaveMaker0 -
Wordpress plugin for facilitating adding a post descriptions
Could anyone recommend a plugin for creating wordpress post descriptions. There is a confusingly large selection of choices in the wordpress plugins directory.
Technical SEO | | catherine-2793880