Search notes:

.htaccess

Create a 301 (Moved permanently) redirection

RewriteEngine On
Redirect 301 /path/to/ressource.html https://renenyffenegger.ch/notes/development/Apache/Server/htaccess
Note: the redirected path seems to be required to be stated absolutely, even if the .htaccess files is placed in the same directory where the redirected ressource is stored.
See also Redirection with HTML.

Enable php within html documents

The following snippet enables PHP within html documents:
AddHandler application/x-httpd-php .html .htm
In previous(?) or other(?) versions of Apache and/or PHP, the following handler needed to be installed:
AddHandler php5-fastcgi .htm .html
On yet another Apache Server, I had to use:
SetHandler php-script

Serve handler.php no matter what was requested

With the following snippet, any request will be served by handler.php.
RewriteEngine on
RewriteRule ^.* handler.php
Within the PHP script, the requested URI can be accessed with $_SERVER['REQUEST_URI'].

DirectoryIndex

If URL is a path only, Serve file.one if existing else serve file.two (if existing).
DirectoryIndex file.one file.two

Force files without suffix to be served as text/html files

Without suffix, Apache is apparently unable to determine a MIME type.
In order to force the MIME type for files without suffix, the following snippet should do:
<FilesMatch "^[^\.]+$">
  ForceType text/html
</FilesMatch>

Create 404 for directory and subdirectory

RewriteEngine On
ErrorDocument 404 https://url.xyz/path/to/404-document.html
Redirect 404 /path/of/directory/to/create/404/for

Set PHP include path

Some sources on the Internet claim that it is possible to set the PHP include path with the following directive:
php_value  include_path  ".:/home/rene/php"
However, when I tried that, I got the error message Invalid command 'php_value', perhaps misspelled or defined by a module not included in the server configuration.
I was able to set the include path with a .user.ini file (to be placed in the same location as the .htaccess file) like so, though:
include_path=".:/home/rene/php"

Let Apache serve suspicious files with names like wget, too.

For security reasons, Apache does by default not really like to serve URIs that contain the word wget etc.
This behaviour can be turned off with the following snippet:
<IfModule mod_security.c>
  SecFilterEngine Off
  SecFilterScanPOST Off
</IfModule>

Redirect HTTP to HTTPS

Redirect a request to http://abc.yz/foo/bar.html to https://abc.yz/foo/bar.html.
In order to take effect for any url, the following .htaccess must pe put in the root directory.
The R=301 indicates a permanent redirect. Just an R would default to 302 (=Found). The L indicates the last rule (see also the .htaccess flags)
Since %{REQUEST_URI} apparently always starts with a slash, there is no need for a slash between the servername and the URI.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://renenyffenegger.ch%{REQUEST_URI} [R=301,L]
The following snippet didn't work in subdirectories. Apparently the regular expression only matches the last part of the URI:
RewriteEngine On
RewriteCond %{SERVER_PORT} !^ 443 $
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L]

Flags

This Stackoverflow answer provides a list of .htaccess flags:
C chained with next rule
CO=cookie set specified cookie
E=var:value set environment variable var to value
F forbidden - sends a 403 header to the user
G gone - no longer exists
H=handler set handler
L last - stop processing rules Last rule: instructs the server to stop rewriting after the preceding directive is processed.
N next - continue processing rules
NC case insensitive
NE do not escape special URL characters in output
NS ignore this rule if the request is a subrequest
P proxy - i.e., apache should grab the remote content specified in the substitution section and return it
PT pass through - use when processing URLs with additional handlers, e.g., mod_alias
R temporary redirect to new URL
R=301 permanent redirect to new URL
QSA append query string from request to substituted URL
S=x skip next x rules
T=mime-type force specified mime type

See also

A corrupt .htaccess file might lead to a 500 - Internal Server Error HTTP status code.
The AllowOverride directive in the httpd.conf controls the set of directives that can be placed in a .htaccess file.

Index