Remove Trailing Slash for Permalinks with Extensions

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):

4 Opinions for Remove Trailing Slash for Permalinks with Extensions

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.

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.