Beginners guide to .htaccess file with examples - Part II
I hope you have gone through the examples listed in first part of this tutorial. If not, it’s here - [Beginners guide to .htaccess file with examples].
In this part, we will cover more examples with .htaccess file.
Presenting custom error pages
Use .htaccess file to present users with your custom pages for 401 [Authorization Required], 403 [Forbidden], 404 [not found] and 500 [Internal Server Error].
Syntax:
ErrorDocument < error-code > < location -of-custom-page>
Examples:
ErrorDocument 401 /401.html
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html
You can include some script in your customized pages to automatically send an email to you whenever those pages are called for. This way you will be notified every time a user encounters 404, 500 and other error messages.
Redirecting your-domain.com[non-www] to www.your-domain.com
From a search engine’s optimization point of view, we will use permanent redirection such that every time a request for non-www domain is made, it’s redirected and returns a status code of 301 [Permanent Redirect].
RewriteEngine On
RewriteCond %{HTTP_HOST} ^YourSite\.com [nc]
RewriteRule (.*) http://www.YourSite.com/$1 [R=301,L]
In the above lines, the first line tells the RewriteEngine to be On, second line mentions the Rewrite Condition and the last one is the rewrite rule.
Limiting Number of simultaneous connections
To limit the number of simultaneous connections to a directory or your entire site, use the below line. If you place it in a directory other than the root directory, then it will limit the connections to that directory and its sub-directories only. Placing it in htaccess file of root directory will implement it for entire site.
Syntax:
MaxClients < number-of-connections>
Examples:
MaxClients 40
MaxClients 100
Allow/Disallow certain visitors from accessing your site
To accomplish it use the following lines. Look at the syntax first:
Syntax:
Order allow,deny
Deny from < incoming -address >
Allow from < incoming -address>
The first line [Order allow,deny] tells what should be done first. The second line tells about denying incoming-addresses [could be a single IP, an IP block, domain name and all] and third line tells about the incoming-addresses [could be a single IP, an IP block, domain name and all] those should be allowed. If second line has ‘Deny all’, then you should change the order of allow,deny in the first line to deny,allow.
To deny access to a single IP address and allow everyone else
Order allow,deny
Deny from 100.100.100.1
Allow from all
To deny a block of IP address and allow everyone else. [Notice the second line]
Order allow,deny
Deny from 100.100.100.
Allow from all
To deny a single IP address and allow everyone else. [Use it to block referrals from a specific domain]
Order allow,deny
Deny from www.my-domain.com
Allow from all
To deny everyone else but yourself. [Notice the order of allow,deny has changed in first line and also appending more 'Allow from' lines at the end, which is valid]
Order deny,allow
Deny from all
Allow from < your-ip-address >
Allow from < another-ip-address >
Allow from < www.domain.com>
Specify Administrator’s email address in error message
Using the line below in your htaccess file will display the conerned system administrator’s email address in the error messages. If you are not using custom pages for error messages, then this will help your visitors drop an email and let you know about problems.
Syntax:
ServerAdmin < a-valid-email-address>
Example:
ServerAdmin name@domain.com
I hope, that this tutorial will help you accomplish various tasks with ease. Your feedback, comments are welcome.

RMStringer Said,
November 27, 2006 @ 12:53 am
i am having problems getting my site to come up. i have purchased space on a Linux server and when i type in my webaddress: http://www.thermstringers.com it will not to the indexpage to show my content. can yopu help?
Raj Said,
November 27, 2006 @ 1:45 am
On linux, index.htm, index.html, index.php are the most common pages that will automatically show as main page. What is the page you are using as your home page? Btw, I see something coming up on your site… is it working now?
Andrew Ferguson Said,
December 11, 2006 @ 2:01 am
Thanks for making this!
It’s coming in really handy for an .htaccess file I’m making for a new domain project.
Specifying the serveradmin’s email is something that I didn’t know you could do with these before.
Raj Said,
December 11, 2006 @ 10:11 am
Hello Andrew,
I appreciate you taking time to post your valuable comments. If there is anything we can help you with your htaccess file, do drop in an email.
Aldian Prakoso Said,
December 13, 2006 @ 5:05 am
Hi Raj, great tips!
I have one question for you, how to redirect from one domain to another domain? I tried “redirect 301 / http://newdomain.com” but it didn’t work. Can you please help? Thanks.
Aldian Prakoso Said,
December 13, 2006 @ 6:18 am
Nevermind Raj. Found it. Silly one: it’s case sensitive. Thanks.
Raj Said,
December 13, 2006 @ 10:31 am
Aldian, you have a good website. I like the template and layout. Don’t you blog regularly? - Raj
Jamie Said,
March 3, 2007 @ 4:33 am
Hi Raj,
When I add “ServerAdmin serveradmin@cardpaymentinfo.co.uk” to my htaccessfile, I receive a “500 Internal Server Error, The server encountered an internal error or misconfiguration and was unable to complete your request.”
Any ideas on what the problem might be?
Raj Said,
March 3, 2007 @ 9:03 am
Hey Jamie,
Is that the last line in your htaccess file or are there lines before ServerAdmin directive ?
Wietse Said,
April 25, 2007 @ 1:16 pm
Thanks for the tips, it’s working for the biggest part now, except a little thing:
I want to automate redirection from
http://www.towerdefence.net/index.php?p=Flashcraft_Tower_Defence_TD
to
http://www.towerdefence.net/Flashcraft_Tower_Defence_TD
automatically. Is this possible?
My current .htaccess:
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /index.php?p=$1 [L]
RewriteCond %{HTTP_HOST} ^towerdefence\.net [nc]
RewriteRule (.*) http://www.towerdefence.net/$1 [R=301,L]
Raj Said,
April 25, 2007 @ 1:29 pm
Hey Wietse,
Instead of using the above lines, use these..
RewriteEngine on
RewriteCond %{HTTP_HOST} ^towerdefence\.net [NC]
RewriteRule (.*) http://www.towerdefence.net/$1 [R=301]
RewriteRule ^([^/\.]+)/?$ /index.php?p=$1 [L]
and let me know if it works?
Wietse Said,
April 25, 2007 @ 1:37 pm
Nope, it had the same effect as the old one.
Thanks for the same response though!
Wietse Said,
April 25, 2007 @ 1:37 pm
-edit post :P-
’same’ had to be fast
Raj Said,
April 25, 2007 @ 1:41 pm
I checked your site, it seems to be working at this moment.
Wietse Said,
April 25, 2007 @ 1:46 pm
Sure?
I knew i can go to :
http://www.towerdefence.net/Flash_Circle_Tower_Defence_Towers
(thanks to your examples)
But what i mean is that that becomes the standard, that links to
http://www.towerdefence.net/index.php?p=Flash_Circle_Tower_Defence_Towers
redirect to
http://www.towerdefence.net/Flash_Circle_Tower_Defence_Towers
because that’s not working for me yet.
Raj Said,
April 25, 2007 @ 1:47 pm
The current links in the sidebar must be modified to take the index.php?p= portion out and you will see that it will work. It’s called URL rewriting.
Give it a try with any of your site’s URL without index.php?p= portion in it.
If that’s not what you have been looking for, send me an email -> raj AT bloghash DOT com
Wietse Said,
April 25, 2007 @ 1:56 pm
Okay, i will change the links on the pages.
But what i really ment was like this:
When i enter towerdefence.net it automaticlly redirects to the http://www. version.
I was hoping that this would be possible with “index.php?p=” to “” (nothing) too.
Ps, you have a typo in your ‘leave a comment’ box:
Name (required)
E-mail required)
URI
URI should be URL
Raj Said,
April 25, 2007 @ 2:09 pm
Hey,
In that case try adding this line to the very beginning of your htaccess file.
Redirect permanent
replace the old url with current url and new url to the url you want it to redirect to.. i have not tried it with options like index.php?p= , so you may want to try and lemme know.
Btw, that URI stands for Uniform Resource Identifier
Wietse Said,
April 25, 2007 @ 2:31 pm
Okay :p
Well, i tried it
(Redirect permanent
RewriteEngine on
RewriteCond %{HTTP_HOST} ^towerdefence.net [NC]
RewriteRule (.*) http://www.towerdefence.net/$1 [R=301]
RewriteRule ^([^/.]+)/?$ /index.php?p=$1 [L])
but with a Internal Server Error to all of the pages.
btw, like you said before, maybe it’s better to have contact via email, instead of spamming your blogg
mine’s wietse89 at gmail .com
Wietse Said,
May 3, 2007 @ 3:50 am
Now i got the most things working, except that i want to add another rewrite to the htaccess:
Current file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^towerdefence.net [NC]
RewriteRule (.*) http://www.towerdefence.net/$1 [R=301]
RewriteRule ^([^/.]+)/?$ /index.php?p=$1 [L]
what it should be:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^towerdefence.net [NC]
RewriteRule (.*) http://www.towerdefence.net/$1 [R=301]
RewriteRule ^([^/.]+)/?$ /index.php?p=$1 [L]
RewriteRule ^([^/.]+)/?$ /playindex.php?p=$1 [L]
So that playindex.php will rewrite the pages aswell. but what i tried didn’t work..
Raj Said,
May 3, 2007 @ 7:19 am
Wietse, that will not work. Your last two lines are being used to perform same rewriting. When you use [L] at the end of the line, rewriting is stopped if the rewriting condition is met. If you take out the [L] from the index.php?=$1 line, it will also use the last line for rewriting, but what is that you are trying to achieve? You are passing the same variables for rewriting to both the files?
Wietse Said,
May 3, 2007 @ 8:20 am
what i want to achieve is this: that i can rewrite
towerdefence.net/playindex.php?p=play-roman-sanine-tower-defence
to
towerdefence.net/play-roman-sanine-tower-defence
and with the current htaccess file only works on the index.php?p= rewrite.
Raj Said,
May 3, 2007 @ 8:46 am
Wietse, you may not be able to achieve that. There is no way you are telling the htaccess to differentiate between two URLs which follow your domain name.
Example:
yourdomain.com/something
yourdomain.com/somethingelse
In both the cases, htaccess will not know to rewrite first or the second URL above using either index.php or playindex.php. I recommend you use something like yourdomain.com/play/URL and then use that to rewrite using playindex.php in your htaccess file.
Wietse Said,
May 3, 2007 @ 8:58 am
okay…
but what do i change if i want to let the link
towerdefence.net/playindex.php?p=play-roman-sanine-tower-defence
redirects to
towerdefence.net/play/roman-sanine-tower-defence
Mark Bowen Said,
May 23, 2007 @ 8:49 am
Hi there,
Just to say great little tutorial on htaccess so thanks for that.
I was just wondering if anyone had any ideas as to why this isn’t working for me?
HTAccess file located in a downloads folder called ‘downloads’
Order deny, allow
Deny from all
Allow from localhost
What I want to be able to do is have all my files to download in the downloads folder. I then tried making a simple html file outside of the folder which linked to an image inside the downloads folder. Both on the same server. I was expecting it to show the image but not to show if I just typed in the address of the image itself.
Unfortunately it won’t show at all either by using the html page or by typing the address to the image itself. I changed the htaccess file so that it didn’t have the period in front of it (just to do a quick test) and the image shows up fine with the html page so I know that this isn’t the problem.
I had a word with my internet hosting provider and they said that it probably wouldn’t work on their servers as they are Apache based but I thought that you HAD to have Apache in order to get this to work anyway??? I can get other parts of htaccess files to work so I am a bit surprised as to why this doesn’t?
I also tried using ‘domainname.com’ and the IP address of the server in place of localhost but none of these seem to work.
Does anyone know if there is anything else I can do with this at all. Basically what I want is if a page on my server provides a link to any file within the downloads folder then the user can view or download the file. If they just try to type in the address to the file it won’t do anything. I would really like to get this working and also NOT to have to use any of the anti-leech scripts out there if at all possible.
Thank you in advance.
Mark
Raj Said,
May 23, 2007 @ 9:50 am
Hi Mark,
Yes, htaccess is an Apache only feature. Your hosting company is not right in saying that it doesn’t work with Apache.
What you are trying to accomplish will not work. Technically, when you only allow localhost, all the files from downloads folder will not be accessible to anyone other than the localhost itself. Even if you setup a html file outside the folder and provide links to the files from downloads folder, whenever anyone clicks on that link, they will be not be localhost, so it will not work this way. If you have CPanel, you may want to use Antileech features to accomplish this.
Reza Said,
May 23, 2007 @ 3:06 pm
hi there…
i am doing a web maintenance ,i saw an error which is said” 500 internal error server”. When i check in the internet i found this error is a .htaccess file. I like to remove this error from the web site because when the customer click on the link it give them that error.I tried to delet the htaccess file from our FTP ,but it did not let me. Please let me know how i should remove this error from the website.
Best regards
Reza
Raj Said,
May 23, 2007 @ 3:54 pm
Hello Reza,
The 500 Internal Server Error does not occur only when something is not right with .htaccess file. You may however try to rename the .htaccess file to something else and check if the error goes away. You may also want to check for error_log file to see if it contains any information for you to debug into.
Hope this helps!
Mark Bowen Said,
May 23, 2007 @ 4:18 pm
Hi Raj,
Thanks for the answer although I am a little confused as I had seen this done from within the PostNuke CMS where they had a downloads module called ‘UpDownload’ I think which created folders to store downloads and within those folders was a htaccess file which stopped anyone from the outside but the PostNuke system could still download from.
Not too sure exactly what was in the htaccess file now as it has been a very long time since I have used that CMS but I’m pretty sure that all they did was deny all and allow localhost and it then served up the files using the module.
Is there absolutely no way that this can be done? I’m sure there must be something. I’m of the opinion that there is just about nothing that can’t be accomplished on the internet nowadays and this just seems almost fundamental. I can’t understand why you can deny access to all and then you can’t even get to the files yourself, not too sure why that would ever be of any help at all?
Any more help on this would be massively appreciated. Also the control panel isn’t cPanel, it is Plesk so unfortunately no Anti-Leech features in there to my knowledge.
Thanks again for any help with this as it is very much appreciated.
Regards,
Mark
Reza Said,
May 23, 2007 @ 4:41 pm
Hello Raj:
Thank you so much for your help. I tried to rename the .htaccess but it did not let me to rename it and i search in their FTP files ,but i did not find any error log.
can you help me more?
Raj Said,
May 24, 2007 @ 12:44 am
@Mark,
I am researching on this. I will send you an email as soon as I find a solution. It would work when downloads folder is denied direct access but allowed when referrer is the same website. I am working on that part.
@Reza,
Looks like a permission issue. You can have your hosting company rename the htaccess file for you and set the permissions accordingly.
Mark Bowen Said,
May 24, 2007 @ 1:55 am
Hi Raj,
You are an absolute star. Thanks for looking into all of this for me it is highly appreciated.
Thanks again.
Best wishes,
Mark
Reza Said,
May 24, 2007 @ 9:24 am
@Raj
Thank you for your information.
Murray Said,
October 2, 2007 @ 1:17 am
Please note, for all versions of Apache, the ServerAdmin directive can only be set in the config or globally in httpd.conf, as per documentation:
http://httpd.apache.org/docs/2.2/mod/quickreference.html
Otherwise you will get a 500 internal server error (Jamie/Raj)
Perhaps this should be removed from this blog?
Murray Said,
October 2, 2007 @ 1:21 am
Sorry, second line supposed to read: …set in the <VirtualHost> config
saleem Said,
April 29, 2008 @ 1:34 am
Hi Raj,
I want to redirect the url like
details.php?county_id=1&mls_number=12345 To
details/dutches/12345
Here if i pass a country_id=1 it should come ‘dutches’ or if i pass country_id=2 it should come ’span’, Please let me give a code for this.
Regards
Saleem
Raj Said,
April 29, 2008 @ 2:53 am
Saleem,
You will find examples of rewriting the URLs here
saleem Said,
April 29, 2008 @ 4:13 am
Thanks for you response, I have already refered and designed the url like this
RewriteRule ^details/([^/\.]+)/([^/\.]+)?$ details.php?country_id=$1&mls_number=$2 [L]
but its not working… please correct it my code
Regards
Saleem