Last active
February 21, 2019 15:29
-
-
Save michael-milette/d3f734fc6897aa93ce0f35be98d65026 to your computer and use it in GitHub Desktop.
Useful Apache 2.4 redirection tips and tricks
This file contains 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 client to use secure HTTPS connections only. | |
#################################################### | |
RewriteEngine on | |
# Use HTTP Strict Transport Security to force client to use secure connections only. | |
RewriteCond %{HTTPS} on | |
RewriteRule ^ - [ENV=HTTPS:true] | |
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" env=HTTPS | |
# If HSTS wasn't supported, rewrite all HTTP to HTTPS. | |
RewriteCond %{HTTPS} off | |
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] | |
#################################################### | |
# Remove any www prefix. | |
#################################################### | |
RewriteCond %{HTTP_HOST} ^www\.(.+) | |
RewriteRule ^(.*)$ https://%1/$1 [E=HTTPS,R=301,L] | |
#################################################### | |
# Redirects | |
#################################################### | |
# Vanity domains to a specific page. | |
RewriteCond %{HTTP_HOST} ^vanity\.yourdomain\.tld$ [NC] | |
RewriteRule ^(.*)$ https://yourdomain/topic/index.html?lang=en [E=HTTPS,R=301,L] | |
# Redirect vanity URLs to a specific page. | |
Redirect 301 /vanity /this/topic/path/index.html | |
# Redirect sections of your website that were moved. | |
RedirectMatch ^/old/path/(.*) /new/path/$1 | |
# Redirect sections of your website that were removed. | |
RewriteRule ^old/path/(.*) "/new/path/index.html" [R=301,NC,L] | |
#################################################### | |
# Redirect vanity domains to primary domain. | |
#################################################### | |
RewriteCond %{HTTP_HOST} !^vanity\.yoursite\.tld$ [NC] | |
RewriteRule ^/?(.*) https://yoursite.tld/$1 [R=301,L] | |
#################################################### | |
# Block access to specific directories / folders. | |
#################################################### | |
# Block access for these folders | |
RedirectMatch 404 (.git|tests|nbproject|.idea) | |
# Block access to specific files | |
RedirectMatch 404 (composer.lock|composer.json|\.htaccess|LICENSE|LICENSE.txt|CHANGELOG|CHANGELOG.txt) | |
# Block all direct access to specific file types: | |
RedirectMatch 404 (.*)\.(md|txt|sh|bat|bak|old|yaml|twig|dist) | |
#################################################### | |
# Redirect for pages that were renamed (global). | |
#################################################### | |
RewriteRule ^index-eng\.html$ /index-en.html [R=301,L] | |
RewriteRule ^(.*)/index-eng\.html$ /$1/index-en.html [NC,E=HTTPS,R=301,L] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment