A collection of useful .htaccess codes.
To work all of these, add the following line at the beginning to your .htaccess file.
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
php_value <key> <val>
php_value upload_max_filesize 50M
php_value max_execution_time 240
ErrorDocument 400 /400.html
ErrorDocument 401 http://error.yourdomain.com/mordor.html
ErrorDocument 403 /403.html
ErrorDocument 404 /errors/halflife3.html
ErrorDocument 405 /405.html
ErrorDocument 408 /408.html
ErrorDocument 414 /414.html
ErrorDocument 500 "Houston, we have a problem."
ErrorDocument 502 /502.html
ErrorDocument 504 /504.html
CDN-served webfonts might not work in Firefox or IE due to [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing). The following snippet from [alrra](https://github.com/h5bp/server-configs-apache/issues/32) should make it happen.
<IfModule mod_headers.c>
<FilesMatch "\.(eot|otf|ttc|ttf|woff|woff2)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
Use UTF-8 encoding for anything served text/plain or text/html
AddDefaultCharset utf-8
Force UTF-8 for a number of file formats
AddCharset utf-8 .atom .css .js .json .rss .vtt .xml
If you’re on a shared host, chances are there are more than one version of PHP installed, and sometimes you want a specific version for your website. For example, [Laravel](https://github.com/laravel/laravel) requires PHP >= 5.4. The following snippet should switch the PHP version for you.
AddHandler application/x-httpd-php55 .php
Alternatively, you can use AddType
AddType application/x-httpd-php55 .php
Compatibility View in IE may affect how some websites are displayed. The following snippet should force IE to use the Edge Rendering Engine and disable the Compatibility View.
<IfModule mod_headers.c>
BrowserMatch MSIE is-msie
Header set X-UA-Compatible IE=edge env=is-msie
</IfModule>
Rather than having to call / include a file you need on every single page, you can have them automatically prepended (top of file) or appended (bottom of file) automatically through your .htaccess file.
php_value auto_prepend_file "/real/path/to/file/functions.php"
php_value auto_append_file "/real/path/to/file/footer.php"
Log errors to a file, and prevent showing them to the user. Make sure that the file exists and youre able to write to it.
# display no errs to user
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
# log to file
php_flag log_errors on
php_value error_log /location/to/php_error.log
The URL in the browser would be:
http://css-tricks.com/index.php/teachers/a/
The actual page rendered by the server would be:
http://css-tricks.com/index.php?search=teachers&sort=a
RewriteEngine on
RewriteRule ^index/([^/]+)/([^/]+).php /page.php?search=$1&sort=$2 [NC]
# Maintenance Redirection
# Replace 555\.555\.555\.555 with your own IP address
# Uncomment first conditional to turn off the redirection
# RewriteCond %{REQUEST_URI} ^$a
RewriteCond %{REQUEST_URI} !maintenance.html
RewriteCond %{REQUEST_FILENAME} !(styles|images).+$
RewriteCond %{REMOTE_ADDR} !^555\.555\.555\.555$
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1$
RewriteRule (.*) /maintenance.html [R,L]
This code makes it easy to temporarily take down a website for updates. Replace the “555” line with your own IP address so that you’ll still be able to view the website as normal while everyone else gets redirected. Images and styles are allowed to pass through the filter as well.
or,
# Redirect all traffic to maintenance.html file
RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123
RewriteRule $ /maintenance.html [R=302,L]
RemoveHandler cgi-script .pl .cgi .php .py
AddType text/plain .pl .cgi .php .py
AddType application/octet-stream .zip .mp3 .mp4
This page wouldn’t have such a long without