Useful Apache Directives for "htaccess" files

These are useful directives you can use in 'htaccess' files.

Also see

SecFilterEngine Off

Turn off url filtering if it interferes with executing your script

SecFilterEngine Off

Also see http://help.joyent.com/index.php?pg=kb.page&id=85

Browse folders/directories

Manage file handling by extension

Tells Apache how to handle files with specific extensions.

AddHandler x-httpd-php5 .php
AddHandler x-httpd-php .php4
# Also parse dhtml files by php?
AddHandler x-httpd-php .dhtml

Force SSL https

Source: http://www.besthostratings.com/articles/force-ssl-htaccess.html
Source: http://www.forum.psoft.net/archive/index.php/t-16393.html
Source: http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
Source: https://www.cheatography.com/davechild/cheat-sheets/mod-rewrite/

## Force https secure connection.
## See https://www.cheatography.com/davechild/cheat-sheets/mod-rewrite/
RewriteEngine On
RewriteCond %{HTTPS} !=on
## HTTP_HOST is from HTTP Header (provided by client browser, can be less secure), 
## SERVER_NAME is from server internal variable (can be more secure, but make sure it is configured properly or it might not work well.
#RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NS,L,R=301] 
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [NS,L,R=301]

For explanation, see http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html#RewriteRule

Alternatively, use following RewriteCond for checking port 443 instead of HTTPS

 RewriteCond %{SERVER_PORT} !=443 

Do you wish to the same by PHP?
https://secure.php.net/manual/en/function.header.php
https://www.w3schools.com/php/func_http_header.asp

<?php
if ($_SERVER[HTTPS]!="on") {
  //$strURIName=$_SERVER['SERVER_NAME'] . getenv("REQUEST_URI"); 
  // The function 'getenv' does not work if your Server API is ASAPI (IIS).
  // So, try to don't use getenv('REMOTE_ADDR'), but $_SERVER["REMOTE_ADDR"].
  $strURIName= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
  header ("Location: https://$strURIName");
  exit;
}

To troubleshoot your PHP code

<?php
echo "<b>_SERVER Variables from $_SERVER</b><br><br>";
reset($_SERVER);
while (list ($key, $val) = each ($_SERVER)) {
  print $key . " = " . $val . "<br>";
}

Redirect Subdomain to Subdirectories

Preserves url, works for both http and https.

http://stackoverflow.com/questions/20307621/redirect-subdomain-to-folder-without-changing-url

# see http://stackoverflow.com/questions/20307621/redirect-subdomain-to-folder-without-changing-url
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www)\.domain\.com$ [NC]
RewriteRule !^www/ /www%{REQUEST_URI} [L,NC]
# L = last rule, 
# NC = No Case, case insensitive,