We have a blog installed at www.lifechurchstlouis.org/blog . Google indexes it without a trailing slash. The canonical url by Yoast SEO has no trailing slash. However, on going to the url, there is a 301 redirect to www.lifechurchstlouis.org/blog/. The htaccess rules are default WP rules. Something is amiss with this common situation. There should not be a 301 redirect from the canonical url to another url. Not sure where this 301 redirect is coming from. If from WP, then Yoast SEO should set the canonical url to have a trailing slash when WP is in a subdirectory.
Sounds like a duplicate of #1336 and #1352. Not closing (yet) as the 301 redirect seems weird.
I have moved this blog from it's own domain to the subdirectory and have apparently been penalized severely by Google. A Level 10 contributor on Google Webmaster Groups has suggested that this lack of a trailing slash and the subsequent 301 redirect is probably the cause of the Google penalty. I see in the other cases, that you dismiss this as no big deal. I think you need to reconsider. You should have code to add the slash to the canonical url for the root directory/url. I see no reasonable case where you would not want the slash.
Let me also add that in the Sitemap, the home url is www.lifechurchstlouis.org/blog without the trailing slash, which is what Google uses for their index. I understand that for sites in the domain root, the presence or absence of the slash doesn't matter. However, for a site in a subdirectory, it matters. This may be partly an issue with WP itself, but hopefully the plugin can fix it so we can keep everything consistent. My understanding is that a trailing slash for the subdirectory is best practice. The Level10 Google contributor said that the 301 redirect adding the slash was from WP itself.
Further info is that the trailing slash appears to be from default Apache settings. If you set DirectorySlash Off in .htaccess, then the redirect does not occur. However, then www.lifechurchstlouis.org/blog gives a 404. So the trailing slash is required. So again, the SEO plugin should put the trailing slash in the canonical url and in the sitemap when the site is in a subdirectory.
Could you test the following to see if that solves it for you ? - add the below line to your .htaccess:
Redirect permanent /blog http://www.lifechurchstlouis.org/blog/
You're not following this at all. I just said that it is Apache that is adding the trailing slash with a 301 redirect. You have just asked me to add something that does the same thing.
The point is that the correct page has the trailing slash. However the SEO plugin lists the canonical url as not having the trailing slash which is incorrect. I would also prefer that the SEO plugin made sure that WP adds the trailing slash so that Apache would not have to do the redirect and that the home page is listed with the trailing slash in the Yoast SEO sitemap.
Actually, if you add the htaccess line you suggested, you get a redirect loop that keeps adding slashes to the url until it errors out.
Actually, you must do a bad job explaining this then, as if you say the url with the slash is correct, either apache adding the slash or the redirect directive should do the trick. So there must be something else going on.
Anyways, you can filter the output to the canonical and to the sitemap, so I suggest you try that, considering you seem to know best.
Sorry if I offended you. Let me explain as clearly as possible.
I repeat:
Anyways, you can filter the output to the canonical and to the sitemap, so I suggest you try that, considering you seem to know best.
To fix this issue, add this code to your functions.php:
function wpseo_canonical_home_url_fix( $canonical_url ) {
// Simply add a trailing slash if it hasn't one
return trailingslashit( $canonical_url );
}
add_filter( 'wpseo_canonical', 'wpseo_canonical_home_url_fix' );
Hi Thomas,
There are few functions.php files in Wordpress. Can you tell me which functions file and where in the file this should be added?
Neil
@NeilSelkirk usually functions.php file of your theme used is implied, thought it's just _typical_ but not _mandatory_ location to place code in.
@Rast thanks very much. I added @thomasvanderbeek code at the bottom of the themes function.php file and it worked!
To fix this issue, add this code to your functions.php:
function wpseo_canonical_home_url_fix( $canonical_url ) { // Simply add a trailing slash if it hasn't one return trailingslashit( $canonical_url ); } add_filter( 'wpseo_canonical', 'wpseo_canonical_home_url_fix' );
This fix adds a trailingslash to all canonicals, where the issue is that it should only add a trailing slash to the homepage (and all mentions of the homepage, i.e. in breadcrumbs and rel next/prev).
Most helpful comment
To fix this issue, add this code to your functions.php: