Created
October 12, 2016 09:46
-
-
Save u01jmg3/35e9e34eeb635dadd6786eb46bac4341 to your computer and use it in GitHub Desktop.
Template Apache `.htaccess` file with common rewrite rules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##================================================ | |
# Prevent directory listing. | |
##================================================ | |
IndexIgnore * | |
##================================================ | |
# Set Charset. | |
##================================================ | |
AddDefaultCharset utf-8 | |
<IfModule mod_headers.c> | |
BrowserMatch MSIE ie | |
Header set X-UA-Compatible "IE=Edge,chrome=1" env=ie | |
<FilesMatch "\.(js|css|gif|png|jpe?g|pdf|xml|svg|eot|ttf|otf|woff2?|ico|htc)$"> | |
# Separate lines required | |
Header unset X-UA-Compatible | |
</FilesMatch> | |
</IfModule> | |
<IfModule mod_rewrite.c> | |
RewriteEngine On | |
##================================================ | |
# Block viewing of PHP Credits. | |
# ?=PHPB8B5F2A0-3C92-11d3-A3A9-4C7B08C10000 | |
##================================================ | |
RewriteCond %{QUERY_STRING} \=PHP[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} [NC] | |
RewriteRule .* - [F] | |
##================================================ | |
# Remove `index.php` from URL. | |
##================================================ | |
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?) | |
RewriteRule ^ /%1 [R=301,L] | |
##================================================ | |
# Push all requests through `index.php`. | |
##================================================ | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteRule . index.php [L] | |
##================================================ | |
# Add trailing slash (including if site within a sub dir). | |
##================================================ | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteCond %{REQUEST_URI} (.+)/$ | |
RewriteRule ^ %1 [L,R=301] | |
##================================================ | |
# Force www. with http:// | |
##================================================ | |
RewriteCond %{HTTP_HOST} ^google\.co\.uk | |
RewriteCond %{HTTPS} !=on | |
RewriteRule .* http://www.%{SERVER_NAME}%{REQUEST_URI} [R=301] | |
##================================================ | |
# Force www. with https:// | |
##================================================ | |
RewriteCond %{HTTP_HOST} ^google\.co\.uk | |
RewriteCond %{HTTPS} =on | |
RewriteRule .* http://www.%{SERVER_NAME}%{REQUEST_URI} [R=301] | |
##================================================ | |
# Certain file types cannot be viewed directly. | |
##================================================ | |
RewriteCond %{REQUEST_URI} (uploads/) | |
RewriteRule ^(.*)\.(txt|rtf|docx?|odt|pdf|bmp|gif|jpe?g|png|tiff?)$ https://%{SERVER_NAME}/process.php [NC,L] | |
##================================================ | |
# File revving: screen.1410180532.css > screen.css | |
##================================================ | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteRule ^(.+)\.(\d+)\.(js|css|gif|png|jpe?g|pdf|xml|svg|eot|ttf|otf|woff2?|ico|htc)$ $1.$3 [L] | |
##================================================ | |
# Maintenance page with excluded IPs. | |
##================================================ | |
RewriteCond "%{TIME_YEAR}%{TIME_MON}%{TIME_DAY}" 20160101 | |
RewriteCond "%{TIME_HOUR}%{TIME_MIN}" >0900 | |
RewriteCond %{REMOTE_ADDR} !^74\.125\.224\.72$ | |
RewriteRule .*$ http://www.example.com/maintenance/ [L] | |
</IfModule> | |
##================================================ | |
# 4xx Client Error | |
##================================================ | |
ErrorDocument 400 error.php?code=400&label=bad_request | |
ErrorDocument 401 error.php?code=401&label=unauthorised | |
ErrorDocument 403 error.php?code=403&label=forbidden | |
ErrorDocument 404 error.php?code=404&label=file_not_found | |
ErrorDocument 405 error.php?code=405&label=method_not_allowed | |
ErrorDocument 408 error.php?code=408&label=request_timeout | |
ErrorDocument 410 error.php?code=410&label=gone | |
ErrorDocument 411 error.php?code=411&label=length_required | |
ErrorDocument 412 error.php?code=412&label=precondition_failed | |
ErrorDocument 413 error.php?code=413&label=request_entity_too_large | |
ErrorDocument 414 error.php?code=414&label=request_uri_too_large | |
ErrorDocument 415 error.php?code=415&label=unsupported_media_type | |
##================================================ | |
# 5xx Server Error | |
##================================================ | |
ErrorDocument 500 error.php?code=500&label=internal_server_error | |
ErrorDocument 501 error.php?code=501&label=not_implemented | |
ErrorDocument 502 error.php?code=502&label=bad_gateway | |
ErrorDocument 503 error.php?code=503&label=service_unavailable | |
ErrorDocument 506 error.php?code=506&label=variant_also_negotiates |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment