From e359543c9a109d02fe48d3b97bc2a7dd5aca0543 Mon Sep 17 00:00:00 2001 From: Alex Weissman Date: Wed, 8 Apr 2015 12:10:02 +0800 Subject: [PATCH] Added Remove Trailing Slash from Arbitrary Paths --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 5c36880..54e36b0 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ What we are doing here is mostly collecting useful snippets from all over the in - [Force HTTPS Behind a Proxy](#force-https-behind-a-proxy) - [Force Trailing Slash](#force-trailing-slash) - [Remove Trailing Slash](#remove-trailing-slash) + - [Remove Trailing Slash from Arbitrary Paths](#remove-trailing-slash-from-arbitrary-paths) - [Redirect a Single Page](#redirect-a-single-page) - [Alias a Single Directory](#alias-a-single-directory) - [Alias Paths to Script](#alias-paths-to-script) @@ -119,6 +120,18 @@ RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L] RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ /$1 [R=301,L] ``` + +### Remove Trailing Slash from Arbitrary Paths +This snippet will redirect paths ending in slashes to their non-slash-terminated counterparts (except for actual directories). +E.g., `http://www.example.com/blog/` -> `http://www.example.com/blog` +This is important for SEO, since it is [recommended to have a "canonical URL" for every page](http://overit.com/blog/canonical-urls). +``` apacheconf +RewriteCond %{REQUEST_FILENAME} !-d +RewriteCond %{REQUEST_URI} (.+)/$ +RewriteRule ^ %1 [L,R=301] +``` +[Source](https://stackoverflow.com/questions/21417263/htaccess-add-remove-trailing-slash-from-url) + ### Redirect a Single Page ``` apacheconf Redirect 301 /oldpage.html http://www.example.com/newpage.html