Skip to content

Instantly share code, notes, and snippets.

@drifterz28
Last active December 13, 2015 21:58
Show Gist options
  • Save drifterz28/4981059 to your computer and use it in GitHub Desktop.
Save drifterz28/4981059 to your computer and use it in GitHub Desktop.
Helpful htaccess snippets that I have found over the years.
## mod rewrite control URI ##
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
## htaccess block all but listed IP's ##
order deny,allow
deny from all
allow from 192.168.1.1
## htaccess block listed IP's ##
order allow,deny
allow from all
deny from 192.168.1.1
## htaccess set global var ##
SetEnv FOO "bar"
## you can remove the file extention, just make sure that mod_rewrite is set. ##
# Remove trailing slashes.
# e.g. example.com/foo/ will redirect to example.com/foo
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
# Redirect to HTML if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
# Redirect to PHP if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
## htaccess change image 404 to show blank image ##
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule \.(gif|jpe?g|png|bmp) /images/0.gif [NC,L]
RewriteCond %{HTTP_HOST} ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ [OR]
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment