Last active
April 1, 2020 05:26
-
-
Save joostvanveen/9b17f625dbd4169e37c5 to your computer and use it in GitHub Desktop.
Force trailing slash for SEO purposes using htaccess 301 redirects (mod_rewrite) - but on GET requests only, to avoid losing POST data on a POST request to a URI without a trailing slash.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Force trailing slash for SEO purposes | |
RewriteEngine On | |
# For GET and HEAD requests only. We do not want to redirect posted forms and such, or we'll lose all POST data! | |
RewriteCond %{REQUEST_METHOD} ^(GET|HEAD)$ | |
# Not for actual files. We do not want to redirect urls like test.jpg to test.jpg/ | |
RewriteCond %{REQUEST_FILENAME} !-f | |
# Redirect to trailing slash if no slash is present in URI | |
RewriteRule ^(.*[^/])$ /$1/ [L,R=301] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment