Back to Top

Remove Trailing Slash for Permalinks with Extensions

Previous Post:

Remove Trailing Slash for Permalinks with Extensions

As a WordPress user, you should have heard about two closely related terms: pretty permalinks and canonical redirection. Pretty permalinks can help your URLs look nicer and more informative, while canonical redirection can help eliminate duplicate content, both can lead to better page ranking on search engines.

The problem is, when you want to create a virtual (fake) page, whose permalink ends in an extension, for examplehttp://example.com/my-fake-page.html1, the canonical redirection will redirect your permalink to something like this:

http://example.com/my-fake-page.html/

This happens because you are using a permalink settings (in Settings → Permalinks) that ends in a trailing slash, i.e. /. With canonical redirection turned on (which should never be turned off actually), any URL not ending in a slash will be appended with one.

The easiest way to solve this issue is to filter the redirect_canonical hook and return false, but whether this approach works or not greatly depends on how you create your virtual page.

Nevertheless, if you add the fake page using the below snippet (or similar):

add_filter('rewrite_rules_array', 'bwp_insertrules');
add_filter('query_vars', 'bwp_insertqv');

// Adding a new rewrite rule
function bwp_insertrules($rules)
{
	$newrules = array();
	$newrules['my-fake-page\.html$'] = 'index.php?fake_page=my-fake-page';
	return $newrules + $rules;
}

// Tell WordPress to accept our 'fake_page' query variable
function bwp_insertqv($vars)
{
	array_push($vars, 'fake_page');
	return $vars;
}

then to disable canonical redirection (for our fake page’s URL only), you can do something like this:

add_filter('redirect_canonical', 'bwp_cancel_redirect_canonical');

// Cancel the redirection for our fake page
function bwp_cancel_redirect_canonical($redirect_url)
{
	$fake_page = get_query_var('fake_page');
	if (!empty($fake_page))
		return false;
}

Now if you re-visit the fake page, i.e. http://example.com/my-fake-page.html, you will see no trailing slash at the end of the URL, or in other words, no canonical redirection has occurred!

References

  1. 98-wordpress-create-fake-pages/ []

Take Social Sharing to
the Next Level with Monarch!

Take Social Sharing to the Next Level with Monarch!
Print Article Trackback Trackback to this Article   Subscribe to Comments RSS Subscribe to Comments RSS

3 Opinions for Remove Trailing Slash for Permalinks with Extensions

  1. User's Gravatar
    1
    BFSO August 7, 2011 at 3:09 pm – Permalink

    Thank for share!
    We can remove trailing slash for permalinks with extensions with simple method:
    Setting permarlink with custom structure, On page Permalink Setting select Custom Structure and type : /%postname%.html or any extensions as you like.

    • User's Gravatar
      2
      Sable October 10, 2011 at 12:07 pm – Permalink

      Super informative writing; keep it up.

  2. User's Gravatar
    3
    sergio April 28, 2012 at 5:24 am – Permalink

    I want to remind you about the quietly silenced bug in WP core, which makes any canonical link to fail. The post_name value is not hardcoded (in DB) with the new permalink structure for the pre-existing posts, but it’s STILL required to build the permalink on-the-fly, which is compared against current url to determine wether the canonical should be written or not.

    The result:
    The test always returns “hey, the url is different than saved!” and the wrong canonical is written. Plus the ones who tried to tell those pulling their hair not knowing what else to try, like me, are banned from the forums so we don’t spill the beans before it’s fixed.
    More details here http://socialblogsitewebdesign.com/fix-your-wordpress-canonical-links-and-redirections-if-no-plugin-fixes-the-issue

    I hope you can add a note about it and make some pressure for it to be fixed.

    The only solution, so far, is to run a plugin to update all the post_name values with the current permalink structure.

    Good luck.

Speak Up Your Mind!

An asterisk (*) indicates a required field and must be filled.




  • Web page and e-mail addresses turn into links automatically.
  • Wrap codes in: <code lang=""></code> or <pre lang="" extra="">
  • Lines and paragraphs break automatically.

Next Post: