Skip to content

Instantly share code, notes, and snippets.

@davidsword
Last active April 14, 2018 04:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidsword/f44f456df878fc2f39ef71e700d0ae52 to your computer and use it in GitHub Desktop.
Save davidsword/f44f456df878fc2f39ef71e700d0ae52 to your computer and use it in GitHub Desktop.
πŸ”₯🍺 HTACCESS CHEAT SHEET

πŸ“˜ LAMP πŸ“˜ MySQL πŸ“˜ Terminal πŸ“• htaccess


REDIRECT A LOCATION

RedirectMatch 301 /old_location(.*) http://newlocation.com/$1

Redirect permanent /old_folder http://www.example.com/newfolder
RedirectMatch "\.html$" http://www.example.com/index.php

RewriteEngine On
RewriteRule ^([_0-9a-zA-Z-]+/)?wordpress/uploads/(.+) files/$2 [L]

MASK A LOCATION

RewriteEngine On
RewriteRule ^short_filename.pdf /wp-content/uploads/real-file-location.pdf [L]

SPEED+ : GZIP COMMON RESOURCES

<IfModule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml

  # Remove browser bugs (only needed for really old browsers)
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent
</IfModule>

EXPIRES CACHING

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 month"
ExpiresByType image/jpeg "access 1 month"
ExpiresByType image/gif "access 1 month"
ExpiresByType image/png "access 1 month"
ExpiresByType text/css "access 1 week"
ExpiresByType text/html "access 1 week"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/javascript "access 1 week"
ExpiresByType text/x-javascript "access 1 week"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 month"
ExpiresDefault "access 1 week"
</IfModule>

REDIRECT ENTIRE SITE

RewriteEngine on
RewriteBase /
RewriteRule /(.*) http://newwebsite.com/ [R=301,L]

BLOCK FILE TYPES

AddType text/plain .pl
AddType text/plain .py
AddType text/plain .pyc
AddType text/plain .pyo
AddType text/plain .pyd
AddType text/plain .rb
AddType text/plain .so
AddType text/plain .cgi

PROTECT IMPORTANT FILES

<files ~ "^.htaccess">
Order allow,deny
Deny from all
</files>
<Files .htaccess>
order allow,deny
deny from all
</Files>
<files wp-config.php>
order allow,deny
deny from all
</files>
<files install.php>
order allow,deny
deny from all
</files>
<files *.ini>
order allow,deny
deny from all
</files>
<files readme.html>
order allow,deny
deny from all
</files>
<files bb-config.php>
order allow,deny
deny from all
</files>
<Files ~ "\.inc$">
  Order allow,deny
  Deny from all
</Files>

WORDPRESS: BLOCK INCLUDES

RewriteEngine On
RewriteBase /
RewriteRule ^wp-admin/includes/ - [F,L]
RewriteRule !^wp-includes/ - [S=3]
RewriteRule ^wp-includes/[^/]+\.php$ - [F,L]
RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F,L]
RewriteRule ^wp-includes/theme-compat/ - [F,L]

FORCE HTTPS

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://example.com/$1 [R,L]

Custom 404 Page

ErrorDocument 404 /_publications/404.html

YES/NO INDEXING

# no
IndexIgnore */*

# yes
Options +Indexes

IF HOST IS: REWRITE LIKE SO (EXAMPLE)

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} example.com
RewriteRule ^robots.txt robots_disallow.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment