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

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


## Force https secure connection.
## See
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

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

 RewriteCond %{SERVER_PORT} !=443 

Do you wish to the same by 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"].
  header ("Location: https://$strURIName");

To troubleshoot your PHP code

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

Redirect Subdomain to Subdirectories

Preserves url, works for both http and https.

# see
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,