Skip to content

Instantly share code, notes, and snippets.

@Jamiewarb
Last active April 13, 2021 16:07

Revisions

  1. Jamiewarb revised this gist Nov 30, 2017. 1 changed file with 53 additions and 23 deletions.
    76 changes: 53 additions & 23 deletions .htaccess
    Original file line number Diff line number Diff line change
    @@ -1,29 +1,59 @@
    # non-www to www redirect
    RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
    RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
    RewriteEngine On
    RewriteBase /

    # www to non-www redirect
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
    ## Whitelisting + Maintenance

    # http to https redirect
    RewriteCond %{HTTPS} !=on
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
    # 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]

    # https to http redirect
    RewriteCond %{HTTPS} on
    RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,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]

    # 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]
    ## Removing file extension

    # 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]
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
  2. Jamiewarb created this gist Apr 26, 2017.
    29 changes: 29 additions & 0 deletions .htaccess
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    # 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]