24 .htaccess Hacks Attack Should Know About




Apache's .htaccess(hypertext access) configuration file can be a very powerful tool in a web developer's toolkit if used properly. It can be found in the webroot of your server and can be easily edited using any text editor. In this article I'm going to show you 20 .htaccess hacks and how to use them.



Before I start with this article I'd like to start by saying that abusing the .htaccess file will hurt the performance of your website. The .htaccess file should only be used if you have no other way to achieve certain things.

Make sure to back up your current .htaccess file before applying any of the following hacks.



1. Prevent Hotlinking


Tired of people using your bandwidth by putting the images hosted on your server on their website? Add the following code at the bottom of your .htaccess file to prevent hotlinking.






Options +FollowSymlinks






#Protect against hotlinking






RewriteEngine On






RewriteCond %{HTTP_REFERER} !^$






RewriteCond %{HTTP_REFERER} !^http://(www.)?domainname.com/ [nc]






RewriteRule .*.(gif|jpg|png)$ http://domainname.com/img/stop_stealing_bandwidth.gif[nc]


2. Block All Requests From User Agents


It's possible to block all unwanted user agents that might be potentially harmful or perhaps just to keep the server load as low as possible.








#Block bad bots






SetEnvIfNoCase user-Agent ^FrontPage [NC,OR]






SetEnvIfNoCase user-Agent ^Java.* [NC,OR]






SetEnvIfNoCase user-Agent ^Microsoft.URL [NC,OR]






SetEnvIfNoCase user-Agent ^MSFrontPage [NC,OR]






SetEnvIfNoCase user-Agent ^Offline.Explorer [NC,OR]






SetEnvIfNoCase user-Agent ^[Ww]eb[Bb]andit [NC,OR]






SetEnvIfNoCase user-Agent ^Zeus [NC]




"" post="" head="">






Order Allow,Deny






Allow from all






Deny from env=bad_bot










3. Redirect Everyone Except Specified IPs


If for some reason you would want to deny everyone or allow only a specific group of IP addresses to access your website, add the following code to your .htaccess file:






ErrorDocument 403 http://www.domainname.com






Order deny,allow






Deny from all






Allow from 124.34.48.165






Allow from 102.54.68.123


4. SEO Friendly 301 Redirects


If you've transferred domain names or wish to redirect a specific page or pages without getting penalty from search engines such as Google, use the following code:









Redirect 301 /d/file.html http://www.domainname.com/r/file.html


5. Creating a Custom Error Page


Are you as tired as me of the default layout of 404 error pages? Well now you can easily create your own and refer to it like this:






ErrorDocument 401 /error/401.php






ErrorDocument 403 /error/403.php






ErrorDocument 404 /error/404.php






ErrorDocument 500 /error/500.php


6. Create an IP Banlist


Tired of getting the same bs comments specific user over and over again? Just ban the bastard like this by adding the following code to your .htaccess file:






allow from all






deny from 145.186.14.122






deny from 124.15


7. Set Default Email Address For Server Admin


Using the following code you can specify the default email address for the server's admin.






ServerSignature EMail






SetEnv SERVER_ADMIN default@domain.com


8. Disable Display of Download Request


Usually when downloading something from a web site, you'll be prompted if you wish to open the file or save it on your hard-disk. To prevent the server from prompting users wether they wish to open or save the file and to just save the file, use the following code:






AddType application/octet-stream .pdf






AddType application/octet-stream .zip






AddType application/octet-stream .mov


9. Protect a Specific File


The following code allows you to deny access to any file you wish by throwing an 403 error when it is trying to be accessed. In the following example I've chosen to protect the .htaccess file by adding an extra layer of security.






#Protect the .htaccess File






"">






order allow,deny






deny from all











10. Compress Components With mod_deflate


As an alternative to compressing files with Gzip, you can use mod_deflate(which is supposively faster). Place the following code at the top of your .htaccess file(tip: you can also add .jpg|.gif|.png|.tiff|.ico mod_deflate those):






"">






".(js|css)$"="">






SetOutputFilter DEFLATE














11. Add Expires Headers


The following code shows you how to add an expiration date on the headers.






<filesmatch ".(ico|pdf|flv|jpg|jpeg|png|gif|swf)$"="">






Header set Expires "Wed, 21 May 2010 20:00:00 GMT"








12. Setting the Default Page


You can set the default page of a directory to the page of your choice. For example in this code the default page is set as about.html instead of index.html






#Serve Alternate Default Index Page






DirectoryIndex about.html


13. Password Protect Your Directories and Files


You can enable password authentication for any directory or file on your server by using the following code:






#password-protect a file







<files secure.php="">






AuthType Basic






AuthName "Prompt"






AuthUserFile /home/path/.htpasswd






Require valid-user






</files>






# password-protect a directory






resides






AuthType basic






AuthName "This directory is protected"






AuthUserFile /home/path/.htpasswd






AuthGroupFile /dev/null






Require valid-user


14. Redirect an Old Domain to a New Domain


By using the .htaccess file you can redirect a old domain name to a new domain by adding the following code into the htaccess file. Basically what it does is it will remap the old domain to the new one.




#Redirect from an old domain to a new domain




RewriteEngine On




RewriteRule ^(.*)$ http://www.domainname.com/$1 [R=301,L]


15. Force Caching


The following code will not directly increase the loading speed of your website. What it will do is, load the content of your site faster when the same user revisits your website by sending 304 status when requested components have not been modified. You can change the cache expiry by changing the number of seconds(it's currently set at 1 day).




FileETag MTime Size




ExpiresActive on




ExpiresDefault "access plus 86400 seconds"


16. Compress Components By Enabling Gzip


By making use of Gzip you can compress files in order to make your website load faster.






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


17. Remove "category" from a URL


To transform this url: http://yourdomain.com/category/blue to -> http://yourdomain.com/blue, just add the following code at the bottom of your .htaccess file.






RewriteRule ^category/(.+)$ http://www.yourdomain.com/$1 [R=301,L]


18. Disable Directory Browsing


To prevent people from accessing any directories that might contain valueble information or reveal security weaknesses(e.g. plugin directories of wordpress), add the following code to your .htacess file:






Options All -Indexes


19. Redirect WordPress Feeds to FeedBurner


The following snippet redirects WordPress' default RSS feed feedburner's feed.






#Redirect wordpress content feeds to feedburner



<ifmodule mod_rewrite.c="">






RewriteEngine on






RewriteCond %{HTTP_USER_AGENT} !FeedBurner    [NC]






RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]






RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds.feedburner.com/yourfeed [R=302,NC,L]








20. Deny Comments from No Referrer Requests


The problem is that bots just post comments about how to increase your private parts all naturally to your blogs without coming from any other site. It's like they fall from the sky. This neat hack prevents people from posting if they did not come from somewhere else(they can comment just fine if they came from e.g. google).






RewriteEngine On






RewriteCond %{REQUEST_METHOD} POST






RewriteCond %{REQUEST_URI} .wp-comments-post\.php*






RewriteCond %{HTTP_REFERER} !.*yourblog.com.* [OR]






RewriteCond %{HTTP_USER_AGENT} ^$






RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]


21. Remove File Extension From URL


Thanks to Kartlos Tchavelachvili for this one. What the following code does is, it removes the .php extension(you can change it to whatever you like e.g. html) in a url. It makes the URL prettier & SEO friendlier.






RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]


22. Remove www from URL


Thanks to Mahalie for the following 2 .htaccess codes.

If you wish to take out the www from your website's URL and transform it from http://www.example.com into http://example.com, add the following to your .htaccess.




#remove www from URI







RewriteEngine On






RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]






RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]


23. Add Trailing Slash to URL


Some search engines remove the trailing slash from urls that look like directories - e.g. Yahoo does it. But - it could result into duplicated content problems when the same page content is accessible under different urls. The following code makes sure there's a slash at the end of your URL:






#trailing slash enforcement






RewriteBase /






RewriteCond %{REQUEST_FILENAME} !-f






RewriteCond %{REQUEST_URI} !#






RewriteCond %{REQUEST_URI} !(.*)/$






RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]


24. Remove the www. from your website's URL


Below I've provided a simple htaccess snippet to forcefully remove the "www" from your website's URL.






# Redirect if www.yourdomain.com to yourdomain.com






rewriteCond %{HTTP_HOST} ^www\.example\.com [NC]




RewriteRule (.*) http://example.com/$1 [R=301,L]


Bazonggier

Bazonggier is a site where you find unique and professional blogger templates, Improve your blog now for free. Kapan Nikah?

Posting Komentar

Lebih baru Lebih lama