.htaccess Guide!
htaccess file allows you to set server configurations for a specific directory. This could be the root directory for your website or an /images or /downloads directory. It is used on the Apache web server. It can also be used on a handful of other web servers…
I’m going to give you a mini-guide on htaccess. Hope you find it useful!
Warning: BEFORE you edit your .htacces file, store a copy of it in case you mess it up!
Redirect to https
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Remove ? in URL parameter and replace with /
RewriteEngine On
RewriteCond %{QUERY_STRING} blog=(.+) [NC]
RewriteRule ^(.*)$ http://www.example.com/Blog-Details.php/%1? [R=301, NC, L]
Custom Error Documents
ErrorDocument 400 https://example.com/error/400
ErrorDocument 401 https://example.com/error/401
ErrorDocument 403 https://example.com/error/403
ErrorDocument 404 https://example.com/error/404
ErrorDocument 503 https://example.com/error/503
Enable Keep-Alive
This makes your website a bit faster, so you should try it out!
Header set Connection keep-alive
AddType text/cache-manifest .manifest
Allow from IP, deny from others
deny from all
allow from 68.225.245.178
Redirect everyone except IP to another page
ErrorDocument 403 http://www.yahoo.com/
Order deny,allow
Deny from all
Allow from 208.113.134.190
Stop Hotlinking
You won’t really need this because of InfinityFree’s Security System
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com/.*$ [NC]
RewriteRule \.(gif|jpg|swf|flv|png)$ https://www.example.com/feed.gif [R=302,L]
SPECIFY CHARACTERS
Denies any request for a url containing characters other than “a-zA-Z0-9.+/-?=&” - REALLY helps but may break your site depending on your links.
RewriteCond %{REQUEST_URI} !^/(wp-login.php|wp-admin/|wp-content/plugins/|wp-includes/).* [NC]
RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ [a-zA-Z0-9\.\+_/\-\?\=\&]+\ HTTP/ [NC]
RewriteRule .? - [F,NS,L]
Remove .php extension
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.php [NC,L]
Compress text files
<IfModule mod_deflate.c>
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
</IfModule>
</IfModule>
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE application/atom+xml \
application/javascript \
application/json \
application/rss+xml \
application/vnd.ms-fontobject \
application/x-font-ttf \
application/x-web-app-manifest+json \
application/xhtml+xml \
application/xml \
font/opentype \
image/svg+xml \
image/x-icon \
text/css \
text/html \
text/plain \
text/x-component \
text/xml
</IfModule>
</IfModule>
Credits: https://htaccesscheatsheet.com
Caching Scheme
# year
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$">
Header set Cache-Control "public"
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
Header unset Last-Modified
</FilesMatch>
#2 hours
<FilesMatch "\.(html|htm|xml|txt|xsl)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>
<FilesMatch "\.(js|css)$">
SetOutputFilter DEFLATE
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
</FilesMatch>
Prevent directory listing
IndexIgnore *
Redirect to www
RewriteCond %{REQUEST_URI} !^/(robots\.txt|favicon\.ico|sitemap\.xml)$
RewriteCond %{HTTP_HOST} !^www\.askapache\.com$ [NC]
RewriteRule ^(.*)$ https://www.askapache.com/$1 [R=301,L]
Dynamically
RewriteCond %{REQUEST_URI} !^/robots\.txt$ [NC]
RewriteCond %{HTTP_HOST} !^www\.[a-z-]+\.[a-z]{2,6} [NC]
RewriteCond %{HTTP_HOST} ([a-z-]+\.[a-z]{2,6})$ [NC]
RewriteRule ^/(.*)$ http://%1/$1 [R=301,L]
All ErrorDocuments!
ErrorDocument 100 /100_CONTINUE
ErrorDocument 101 /101_SWITCHING_PROTOCOLS
ErrorDocument 102 /102_PROCESSING
ErrorDocument 200 /200_OK
ErrorDocument 201 /201_CREATED
ErrorDocument 202 /202_ACCEPTED
ErrorDocument 203 /203_NON_AUTHORITATIVE
ErrorDocument 204 /204_NO_CONTENT
ErrorDocument 205 /205_RESET_CONTENT
ErrorDocument 206 /206_PARTIAL_CONTENT
ErrorDocument 207 /207_MULTI_STATUS
ErrorDocument 300 /300_MULTIPLE_CHOICES
ErrorDocument 301 /301_MOVED_PERMANENTLY
ErrorDocument 302 /302_MOVED_TEMPORARILY
ErrorDocument 303 /303_SEE_OTHER
ErrorDocument 304 /304_NOT_MODIFIED
ErrorDocument 305 /305_USE_PROXY
ErrorDocument 307 /307_TEMPORARY_REDIRECT
ErrorDocument 400 /400_BAD_REQUEST
ErrorDocument 401 /401_UNAUTHORIZED
ErrorDocument 402 /402_PAYMENT_REQUIRED
ErrorDocument 403 /403_FORBIDDEN
ErrorDocument 404 /404_NOT_FOUND
ErrorDocument 405 /405_METHOD_NOT_ALLOWED
ErrorDocument 406 /406_NOT_ACCEPTABLE
ErrorDocument 407 /407_PROXY_AUTHENTICATION_REQUIRED
ErrorDocument 408 /408_REQUEST_TIME_OUT
ErrorDocument 409 /409_CONFLICT
ErrorDocument 410 /410_GONE
ErrorDocument 411 /411_LENGTH_REQUIRED
ErrorDocument 412 /412_PRECONDITION_FAILED
ErrorDocument 413 /413_REQUEST_ENTITY_TOO_LARGE
ErrorDocument 414 /414_REQUEST_URI_TOO_LARGE
ErrorDocument 415 /415_UNSUPPORTED_MEDIA_TYPE
ErrorDocument 416 /416_RANGE_NOT_SATISFIABLE
ErrorDocument 417 /417_EXPECTATION_FAILED
ErrorDocument 422 /422_UNPROCESSABLE_ENTITY
ErrorDocument 423 /423_LOCKED
ErrorDocument 424 /424_FAILED_DEPENDENCY
ErrorDocument 426 /426_UPGRADE_REQUIRED
ErrorDocument 500 /500_INTERNAL_SERVER_ERROR
ErrorDocument 501 /501_NOT_IMPLEMENTED
ErrorDocument 502 /502_BAD_GATEWAY
ErrorDocument 503 /503_SERVICE_UNAVAILABLE
ErrorDocument 504 /504_GATEWAY_TIME_OUT
ErrorDocument 505 /505_VERSION_NOT_SUPPORTED
ErrorDocument 506 /506_VARIANT_ALSO_VARIES
ErrorDocument 507 /507_INSUFFICIENT_STORAGE
ErrorDocument 510 /510_NOT_EXTENDED
Additional info
#
Forces the server to ignore the text following the # on the same line. Typically used for comments
[F]
Indicates Forbidden, with this the server should return a 403 forbidden error to the client
[L]
The Last rule forces the server to stop processing rules in the .htaccess file
[N]
Indicates Next and forces Apache to redo the rewrite process, except using the currently rewritten URL instead of the initial URL
[G]
Gone tells the server to deliver the gone status message, which is used to mark pages that no longer exist on the site.
[R]
This forces Apache to initialize a redirect, this can be a permanent redirect (page has moved, 301), or a temporary redirect (302).
[P]
Indicates Proxy which tells the server to use mod_proxy to handle requests
[C]
Tells the server to chain a rule with the next rule. If the rule matches for example, then the chained rules will run as well, if not, then they will not run.
[QSA]
Tells the server to use the query string at the end of an expression
[NC]
No Case instructs the server to treat any argument as case insensitive
[NS]
The No Subrequest forces the server to skip if it is an internal subrequest
[PT]
Pass-Through has mod_rewrite send a formatted URL back to Apache
[NE]
No Escape forces the server to parse through all output ignoring escaping characters, meaning spaces in the URL will not be replaced with %20 for example
[OR]
Specifies a logical ‘OR‘ statement that evaluates two expressions
[S=x]
Forces the server to skip “x” number of rules based on if a match is found, not the same as the Chain flag [C]
[a-z]
Denotes a range of characters between the two characters separated by a dash
[^]
Defines not within a character class, or the Start of a string of characters
[]+
Defines that any combination of characters defined within the brackets is a match there can be multiple matches
[]
Defines that any characters defined within the brackets are a match
[T=MIME-type]
Defines the mime type, forces the target file to be that mime type
[E=variableName:newValue]
Forces the server to set the environmental variable “variableName” to the value “newValue”
a{n}
Defines the specific number of the preceding character to be matched
?
Defines the preceding character as being optional
$
Signals the end of a regular expression
()
Can be used to group characters together
^
Signals the beginning of a regular expression
.
Specifies a single arbitrary character
–
Signals not to perform an action
!
Defines negation
+
Will match at least one preceding character
|
Logical ‘OR‘ operator
*
Wildcard that will match any occurrence of the preceding character
.
Signals an escaped literal period
-d
Analyzes if a string exists within a directory
-f
Determines if a string is a preexisting file
-s
Tests for a non zero value
CO=cookie
(set a specified cookie)
Thanks for seeing this post! Hope you found it useful!