Useful Snippets for .htaccess 07-31-2013, 10:03 PM
#1
Hello HC!!
In this tutorial i'll show you 6 wonderful snippets you can add to your .htaccess in order to do magic things!
But Mega, what is a .htaccess?
.htaccess is the file which control your Apache Webserver. It is located in your root directory, ready to be edited! It's very useful and allows you to do a lot of things. In this article, we are going to see 6 snippets that anyone should use
WARNING: Backup! Always backup! If things go wrong, you can restore it if needed.
Removing extensions from url's
I dont know about you, but i hate when i see .html or .php in my url's! The following snippet will remove them for you:
Remove www in url
The following snippet will remove thw "www" from your url. Very useful for SEO.
Create custom error pages
This snippet will redirect you to the following error page, but you need to create them first and upload them in your webserver.
Prevent directory listing
On your web server, when a directory do not have an index file, Apache automatically create a list of all files from the current directory. If you do not wish that anyone can see which files are on your server, just add the following code to your .htaccess file to prevent automatic directory listing.
This is a very important security measure. It prevents attackers from obtaining information as they browse through your directories.
Reduce pages weight by compressing static data
Do you know that it is possible to send compressed data to the visitors, which will be decompressed by the client? This code will definitely save you (and your visitor) bandwidth and reduce your pages weight.
Automatically add utf-8 charset to files
In order to avoid encoding problems, you can force a specific encoding directly on your .htaccess file. That way, you’ll ensure that your html documents will always render correctly, even if your forget to add a <meta http-equiv="Content-Type"> directive on your html pages.
Hope you liked it!
Credits: css-tricks.com, askapache.com, catswhocode.com and myself
In this tutorial i'll show you 6 wonderful snippets you can add to your .htaccess in order to do magic things!
But Mega, what is a .htaccess?
.htaccess is the file which control your Apache Webserver. It is located in your root directory, ready to be edited! It's very useful and allows you to do a lot of things. In this article, we are going to see 6 snippets that anyone should use

WARNING: Backup! Always backup! If things go wrong, you can restore it if needed.
Removing extensions from url's
I dont know about you, but i hate when i see .html or .php in my url's! The following snippet will remove them for you:
Code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Replace html with your file extension, eg: php, htm, aspRemove www in url
The following snippet will remove thw "www" from your url. Very useful for SEO.
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^mywebsite.com$ [NC]
RewriteRule ^(.*)$ http://mywebsite.com/$1 [L,R=301]Create custom error pages
This snippet will redirect you to the following error page, but you need to create them first and upload them in your webserver.
Code:
ErrorDocument 400 /error/badreq.html
ErrorDocument 401 /error/autherror.html
ErrorDocument 403 /error/forbidden.html
ErrorDocument 404 /error/notfound.html
ErrorDocument 500 /error/serverr.htmlPrevent directory listing
On your web server, when a directory do not have an index file, Apache automatically create a list of all files from the current directory. If you do not wish that anyone can see which files are on your server, just add the following code to your .htaccess file to prevent automatic directory listing.
Code:
Options -IndexesReduce pages weight by compressing static data
Do you know that it is possible to send compressed data to the visitors, which will be decompressed by the client? This code will definitely save you (and your visitor) bandwidth and reduce your pages weight.
Code:
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/htmlAutomatically add utf-8 charset to files
In order to avoid encoding problems, you can force a specific encoding directly on your .htaccess file. That way, you’ll ensure that your html documents will always render correctly, even if your forget to add a <meta http-equiv="Content-Type"> directive on your html pages.
Code:
<FilesMatch "\.(htm|html|css|js)$">
AddDefaultCharset UTF-8
</FilesMatch>Hope you liked it!
Credits: css-tricks.com, askapache.com, catswhocode.com and myself
Yar-har Fiddle-dee-dee, being a pirate is alright to be! Do what you want cause a pirate is free, you are a pirate! AHOY!
![[Image: tumblr_m7isvt2N171ranhnao1_500.gif]](http://25.media.tumblr.com/tumblr_m7isvt2N171ranhnao1_500.gif)
![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)