Skip to content

Instantly share code, notes, and snippets.

@jmillerdesign
Created January 17, 2015 22:10
Show Gist options
  • Save jmillerdesign/a80bdca5ce3e34322594 to your computer and use it in GitHub Desktop.
Save jmillerdesign/a80bdca5ce3e34322594 to your computer and use it in GitHub Desktop.
Apache add or remove trailing slash for non-file URLs
# ----------------------------------------------------------------------
# Add trailing slash to (non-file) URLs
# Rewrite "example.com/foo -> example.com/foo/"
# ----------------------------------------------------------------------
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ /$1/ [R=301,L]
</IfModule>
# ----------------------------------------------------------------------
# Remove trailing slash from (non-file) URLs
# Rewrite "example.com/foo/ -> example.com/foo"
# ----------------------------------------------------------------------
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)/$ /$1 [R=301,L]
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment