Archive for category Apache
htaccess Deny Diractory Access During a Specific Time
Posted by admin in HTTP Server on April 27, 2008
If for some reason you would like to block access to files in a directory during a specific time of day, you can do so by adding the following code to an .htaccess file.
RewriteEngine On
# If the hour is 16 (4 PM)
RewriteCond %{TIME_HOUR} ^16$
# Then deny all access
RewriteRule ^.*$ - [F,L]
In the above example, if the hour is 16 (4 PM), the all access would be denied. You can also block multiple hour:
RewriteEngine On
# Multiple hour blocks
# If the hour is 4 PM or 5 PM or 8 AM
RewriteCond %{TIME_HOUR} ^16|17|08$
# Then deny all access
RewriteRule ^.*$ - [F,L]
htaccess Limit the Number of Concurrent Visitors to your Website
Posted by admin in HTTP Server on April 20, 2008
You may want to limit the number of concurrent visitors to your website to prevent server overload or for other reasons. If you need to limit the amount of concurrent surfers to your website, this can be easily set up. Open the htaccess file with a program such as Notepad and insert the following line of code:
MaxClients <Number of max clients>
Change <Number of max clients> to the maximum number of clients you want to allow access to your website.
htaccess Explicitly Define Default Index File
Posted by admin in HTTP Server on April 20, 2008
When a surfer accesses your website, the server checks the root folder for an index file. Some examples of common index files are: index.html, index.htm, index.php, index.cgi, index.pl. The supported index files depend on the how the server is set up.
Your default web hosting server might be set to:
DirectoryIndex index.html index.shtml index.php index.htm default.html Default.htm default.html Default.html default.shtml Default.shtml page1.html index.pl index.cgi index.php3 index.phtml home.htm home.html home.shtml index.wml
As long as you name your “index” file any one of those things, it will work!
If you have two files with names from that list, Apache will show the one that shows up first (e.g. index.htm will show up, even if you have an index.php file in the same directory).
You can change your own DirectoryIndex setting to be anything you’d like via an .htaccess file too!
If the server cannot find an index file, it will try to display an index of all the files within the current directory, however if this is disabled, the server will end up displaying a 403 forbidden error. Using .htaccess, you can use a completely different index file instead of the defaults set by your server host (if you are using a hosting plan). To do this, insert the following line into an .htaccess file:
DirectoryIndex pagename.html second-choice.html third-choice.php
Change pagename.html, second-choice.html third-choice.php to the page that you would like to use as the index file.
Hope this helps!
Block people from Certain URL Using htaccess
Posted by admin in HTTP Server on April 14, 2008
From time to time, you may ask yourself: How do I block people coming from a certain website or URL from visiting my site or directory?
It’s actually very similar to blocking people by IP! Again, you need to add some lines to an .htaccess text file that you create in the home directory of your web site.
Here is some example code for giving everybody who comes to you from www.yahoo.com or www.google.com a 403 error (access denied):
SetEnvIfNoCase Referer "^http://www.google.com/" BadReferrer
SetEnvIfNoCase Referer "^http://www.yahoo.com/" BadReferrer
order deny,allow
deny from env=BadReferrer
Another way to block people where you end up just redirecting them to a different url involves using the “mod_rewrite” functionality of our web server. Here’s how to block everybody from www.yahoo.com and www.google.com again (put this in your .htaccess file):
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://www.yahoo.com/
RewriteRule /* http://www.yoursite.com/restricted_url.html [R,L]
RewriteCond %{HTTP_REFERER} ^http://www.google.com/
RewriteRule /* http://www.yoursite.com/restricted_url.html [R,L]
We hope this helps keep those annoying people out of your site!
Google Text Translation Using htaccess
Posted by admin in HTTP Server on April 14, 2008
Just like many other amazing Google products like Google Maps and Gmail, Google’s Language Tools serves as an excellent online translator that remains fast, effective, and (best of all) free. Another excellent online translator service is provided by Google’s rival, Yahoo! BabelFish, which is also highly efficient and free of charge.
I am going to present a very useful trick for things you can or may want to do with Google’s Language Tools using htaccess. You can make a automatic translation page via htaccess, in another words to say, you can serve alternate language versions of your site content with just a few lines in your htaccess file.
a. For instance, if you want Español and Deutsche version of your web page, in your site’s root .htaccess file, add the following lines of code:
# Automatic Language Translation Options +FollowSymlinks RewriteEngine on RewriteRule ^(.*)-es$ http://www.google.com/translate_c?hl=es&sl=en&u=http://domain.com/$1 [R,NC] RewriteRule ^(.*)-de$ http://www.google.com/translate_c?hl=de&sl=en&u=http://domain.com/$1 [R,NC]
Ensure that you have replaced the instance of “domain.com” with your domain name. You will also want to customize according to which languages you wish to provide translations. In each of the last two lines of the code above, there are two instances of the abbreviation for the language to be translated via that particular line. To customize your set of languages to be translated, simply edit both of the language abbreviations in each line according to the corresponding country abbreviations. Add or delete languages by adding or deleting their corresponding .htaccess expressions.
b. Assume you have a English version HTML page of example.html, you can add in the following two links.
<a href="example.html-es" title="Traducir a Español">Traducir a Español</a> <a href="example.html-de" title="In Deutschen übersetzen">In Deutschen übersetzen</a>
Notice the two-digit language abbreviation hyphenated to the end of each anchor reference. Click on the link, and the users will be able to see the translated pages.
Block IPs Using htaccess
Posted by admin in HTTP Server on April 13, 2008
How do I block certain IPs from accessing my site or directory? It’s pretty easy! All you have to do is create an .htaccess file in the directory you’d like to restrict (your main directory to restrict the entire site) and then put the following in it:
order allow,deny
allow from all
deny from 123.142.124.152
deny from 124.24.
You can put whole ips or just the beginning part you’d like to match, and you can add more and more ips, each with its own line!
When somebody’s ip is banned, they will get a 403 error (access forbidden) when trying to visit your site.
Set Local Timezone Using htaccess
Posted by admin in HTTP Server on April 13, 2008
You may have your web server located in the US or UK, but the target audience might be in elsewhere around the world. And I am sure you want the set local timezone instead of using the server’s timezone. For instance, place the following code in the htaccess file, This will allow PHP to out put current Chicago time instead of the server’s time.
SetEnv TZ America/Chicago
A list of timezones could be found at: http://us2.php.net/manual/en/timezones.php
Use htaccess with Site Maintenance Page
Posted by admin in HTTP Server on April 13, 2008
Occasionally, your site might be under maintenance, and you want to redirect people who try to access different areas of your website to a notice page, for instance: maintenance.html. Place the code below into the htaccess file to make it work.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteRule $ /maintenance.html [R=302,L]
When your are done with maintenance just comment it out with the # symbol. You will also need to make the page maintenance.html
Use htaccess to Deny Access Directory Listing
Posted by admin in HTTP Server on March 24, 2008
Don’t have an index in all your directory’s? Many people create a blank index.html file in every directory to prevent directory listing. It’s ok for a small website, but imagine if you have a website that has hundred or thousand directories.
The easiest way is to write a htaccess file, include the following line in your .htaccess file to deny access to all the directory listings, if there is no index file.
Options -Indexes
Use htaccess to Fake Different File Extension
Posted by admin in HTTP Server on March 24, 2008
Sometimes you want to confuse potential hackers by faking your file extension, this may reduce automated hacker attacks. If you have Apache server and you are allowed to write .htaccess file, write the following code in your .htaccess file.
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^(.+)\.html$ /$1.php [NC,L]
In the example above, it will allow you to refer to your files as example.html instead of example.php










































