title: .htaccess notes tags: apache apache2 htaccess # htaccess notes ## Examples using RewriteEngine ## Force https ``` RewriteEngine on RewriteCond %{HTTPS} !=on RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L] ``` ### Simple cookie based gatekeeper ``` RewriteEngine On RewriteBase /path/to/ RewriteRule \.html$ gatekeeper.php [L] ``` then `gatekeeper.php` ```php bool if( !auth() ) { http_response_code(403); echo "Access denied"; exit(); } # if we get this far, just dump the file if( is_file($filename) ) { readfile($filename); exit(); } else { http_response_code(404); echo "$filename not found."; exit(); } ``` ### Star redirect So that everything goes through a single, central `.php` file ``` RewriteEngine On RewriteBase / RewriteRule ^index.php index.php [L] RewriteRule ^(.*)$ index.php [L,QSA] # everything handled by the index ``` and then `index.php` is simply ```php