Skip to content

Instantly share code, notes, and snippets.

@Jamiewarb
Last active April 13, 2021 16:07
Show Gist options
  • Save Jamiewarb/481e16ae0eb02ab92773de668841c7b8 to your computer and use it in GitHub Desktop.
Save Jamiewarb/481e16ae0eb02ab92773de668841c7b8 to your computer and use it in GitHub Desktop.
Common .htaccess, whitelisting, maintenance, protocol redirects, filetype removal
RewriteEngine On
RewriteBase /
## Whitelisting + Maintenance
# Whitelist IPs to a single page, redirect all else
RewriteCond %{REMOTE_ADDR} !^000\.000\.000\.000
RewriteCond %{REMOTE_ADDR} !^000\.000\.000\.000
RewriteCond %{REQUEST_URI} ^/whitelist-me.php$
RewriteRule ^(.*)$ https://domain.com [R=307,L]
# Maintenance page with whitelisting IP and some assets used on the page
RewriteCond %{REMOTE_ADDR} !^000\.000\.000\.000
RewriteCond %{REQUEST_URI} !^/maintenance\.php$
# Allow a specific requ
RewriteCond %{REQUEST_URI} !^/company-logo.png$ [NC]
# Allow a series of filetypes
RewriteCond %{REQUEST_URI} !\.(css|jpe?g?|png|gif|woff|ttf) [NC]
RewriteRule ^(.*)$ https://www.domain.com/maintenance.php [R=307,L]
## Protocol Redirects
# non-www to www redirect
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
# www to non-www redirect
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# http to https redirect
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# https to http redirect
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#######
# http to https redirect && www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# https to http redirect && www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
## Removing file extension
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment