Skip to content

Instantly share code, notes, and snippets.

@Vlasterx
Created October 18, 2016 11:03
Show Gist options
  • Save Vlasterx/f3e62362788d1e0ef3c78f47e256b2ba to your computer and use it in GitHub Desktop.
Save Vlasterx/f3e62362788d1e0ef3c78f47e256b2ba to your computer and use it in GitHub Desktop.
Common .htaccess with HTTPS enforcing, WWW redirection and common exploit blocking
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
## Begin - Check site protocol HTTP or HTTPS
# initialization code - put only once at the beginning of .htaccess
# then use %{ENV:proto} in your rules
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"' [OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [env=proto:http]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"https"' [OR]
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=proto:https]
#
## End - Check site protocol
## Begin - Enforce WWW before domain name
#
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ %{ENV:proto}://www.%{HTTP_HOST}/$1 [R=301,L]
#
## End - Enforce WWW before domain name
## Begin - Enforce SSL
#
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#
## End - Enforce SSL
## Begin - Rewrite rules to block out some common exploits.
#
# Block out any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
# Block out any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root homepage
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment