Login Register


Useful Snippets for .htaccess filter_list
Author
Message
Useful Snippets for .htaccess #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 Smile




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, asp


Remove 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.html



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.

Code:
Options -Indexes
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.

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/html


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.

Code:
<FilesMatch "\.(htm|html|css|js)$"> AddDefaultCharset UTF-8 </FilesMatch>




Hope you liked it!

Credits: css-tricks.com, askapache.com, catswhocode.com and myself Smile
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]

Reply

RE: Useful Snippets for .htaccess #2
Nice and usefull post.
I'll be sure to bookmark this and share it with people that question this.

I get these kind of questions alot Smile

Reply

RE: Useful Snippets for .htaccess #3
Awesome tutorial.
Just one more addition I'd like to ask you to add :
Code:
Options -Indexes
Its a very important security measure.
[Image: 2YpkRjy.png]
PM me if you need help.
My pastebin HERE. My URL Shortener HERE.

Reply

RE: Useful Snippets for .htaccess #4
Quote:Nice and usefull post.
I'll be sure to bookmark this and share it with people that question this.

I get these kind of questions alot Smile

Thanks! :Smile:


Quote:Awesome tutorial.
Just one more addition I'd like to ask you to add :
Code:
Options -Indexes
Its a very important security measure.

Thanks man! Just edited the post with your suggestion. Smile
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]

Reply

RE: Useful Snippets for .htaccess #5
very userful for SEO . yse or no ,

Reply

RE: Useful Snippets for .htaccess #6
Nicely done. I have another useful tip to chime in.

You can restrict access to a particular folder according to IP address by adding:

Code:
order deny,allow allow from 10.0.1.2 deny from all

Needless to say, you must have a static IP address in order for this to be effective.

As an example, Wordpress users might do this to prevent anybody but them from accessing the admin dashboard.

Similarly, you can also ban arbitrary IP's by using:

Code:
<Limit GET POST> order allow,deny deny from 10.0.20.3 deny from 10.0.20.4 deny from 10.0.20.5 # etc... allow from all </Limit>

Reply

RE: Useful Snippets for .htaccess #7
I found this very usefull! Thank you Smile
Where I live... its freaking cold!
[Image: HC_Signature.png]

Reply

RE: Useful Snippets for .htaccess #8
I found this very usefull! Thank you Smile
Where I live... its freaking cold!
[Image: HC_Signature.png]

Reply

RE: Useful Snippets for .htaccess #9
I found this very usefull! Thank you Smile
Where I live... its freaking cold!
[Image: HC_Signature.png]

Reply







Users browsing this thread: