Skip to content

Instantly share code, notes, and snippets.

@markbosky
Last active December 9, 2021 21:42
Show Gist options
  • Select an option

  • Save markbosky/feb140e7a8a3214818696e98eb12745e to your computer and use it in GitHub Desktop.

Select an option

Save markbosky/feb140e7a8a3214818696e98eb12745e to your computer and use it in GitHub Desktop.
Apache NGINX Server Rules: Enforce Trailing Slash, Redirect to HTTPS
#Apache
<IfModule mod_rewrite.c>
RewriteEngine On
# Ensure all URLs have a trailing slash.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpg|png|jpeg|css|xml|txt|js|php|scss|webp|mp3|avi|wav|mp4|mov|pdf|html|htm|xst)$ [NC]
RewriteRule ^(.*)$ https://domain.com/$1/ [L,R=301]
</IfModule>
#NGINX
#rewrite ^([^.]*[^/])$ https://domain.com$1/ permanent;
# Force Trailing Slash (less REST API calls and files)
if ($request_uri !~ "^/wp-json") {
rewrite ^([^.]*[^/])$ https://domain.com$1/ permanent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment