Skip to content

Instantly share code, notes, and snippets.

@joostvanveen
Last active April 1, 2020 05:26
Show Gist options
  • Save joostvanveen/9b17f625dbd4169e37c5 to your computer and use it in GitHub Desktop.
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.
# 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