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
-
Unexplainable 404 in my Wordpress Monitor
Hi Guys, We started a new website (far from complete) and i'm fixing some SEO problems. I have a SEO 404 monitor plugin and he gives me strange 404's that i can't find: https://compleetverkleed.nl/feestkleding/www.compleetverkleed.nl/feestkleding
Technical SEO | | Happy-SEO
https://compleetverkleed.nl/rode-peper-pak/www.compleetverkleed.nl/feestkleding
https://compleetverkleed.nl/new-kids-kleding/www.compleetverkleed.nl/feestkleding
https://compleetverkleed.nl/fee-kostuum/www.compleetverkleed.nl/feestkleding The 404 error code comes due to a double domain URL, and it happens only on the /feestkleding subdomain. When i check my original page, for example https://compleetverkleed.nl/rode-peper-pak/ there is only 1 internal link to /feestkleding but when i click on it, it works fine. Does anyone know how to fix this problem or where to look to see why this is happening? (it happens on every page, not the 4 above > starting to come through in webmastertools also) 2.
https://compleetverkleed.nl/pa_categorie-sitemap.xml
https://compleetverkleed.nl/product_shipping_class-sitemap.xml
https://compleetverkleed.nl/featured_item_category-sitemap.xml
https://compleetverkleed.nl/post_tag-sitemap.xml
https://compleetverkleed.nl/featured_item_tag-sitemap.xml This 404's are completely new for me and i don't know where to find this 😞 Thanks!2 -
Would removing or making non relevant links no follow boost a site?
Hi, I have just been checking out the backlinks for a prospective new client. It appears they have a number of links that are totally irrelevant to their nature of business and I was wondering if they would improve in the rankings etc if I removed them or made them no follow instead? Or would I simply just be throwing away crucial link juice? Thanks in advance
Technical SEO | | Benjamin3790 -
Removing links from another site
Hello, Some site that I have never been able to access as it is always down has over 3,000 links to my website. They disappeared the other week and our search queries dramatically improved but now they are back again in Google Webmaster and we have dropped again.I have contacted the site owner and got no response and I have also put in a removal form (though I am not sure this fits for that) and asked Google to remove as they have been duplicating our content also. It was in my pending section but has now disappeared.This links are really damaging our search and the site isnt even there. Do I have to list all 3,000 links in the link removal to Google or is there another way I can go about telling them the issue.Appreciate any help on this
Technical SEO | | luwhosjack0 -
WordPress E-Commerce Plugin Duplicate Content Problem
I am working on a wordpress website that uses the WP E-Commerce plugin. I am using the Yoast seo plugin but not totally familiar with it. I have noticed that WP E-Commerce creates duplicate content issues. Here's an example: http://www.domain.com/parent-category/product-url-1/ is the same content as http://www.domain.com/parent-category/child-category/product-url-1/. I was wondering which of these following options are the best solution: 1. 301 redirect the multiple instances to one page
Technical SEO | | theanglemedia
2. noindex all but one instance
3. Use the canonical tag (i've used this tag before for telling SE's to use the www version of a page but not sure if it's the appropriate for this)
4. a combination of one of these 3 options? Thanks in advance!0 -
Does removing product listings help raise SERP's on other pages?
Does removing content ever make sense? We have out of stock products that are left on the site (in an out of stock section) specifically for SEO value, but I am not sure how to approach the problem from a bottom line conversion stand point. Do we leave out of stock products and hope that they turn into a conversion rate via cross selling, or do out of stock products lower the value of other pages by "stealing" link juice and pagerank from the rest of the site? (and effectively driving interest away) What is your perspective? Do you believe that any content that is related or semi-related to your main focus is beneficial, or does it only make sense to have strong content that has a higher rate of conversion and overall site engagement?
Technical SEO | | 13375auc30 -
How do you manage Wordpress URL hierarchy with permalinks?
I have quite a few website in Wordpress but I continuously run into the same issue. With permalinks it is not recommended to use /%category%/%post_name%/ because it puts an undue load on your bandwidth, server and makes the crawler crawl a ton of duplicate content pages. On one site changing to that hierarchy even crashed some of the pages (probably a permissions error). I would like a correct information hierarchy, but this doesn't seem like correct play. What do you use as your URL hierarchy?
Technical SEO | | MarloSchneider
Do you have any plugins or fixes for this issue? Thanks0 -
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