IT Community - Software Programming, Web Development and Technical Support

Using .htaccess files in apache

This is a discussion on Using .htaccess files in apache within the Server Management forums, part of the Servers and Hosting category; With the help of .htaccess files we can override the apache web servers config rules. These file types will hidden ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Servers and Hosting > Server Management

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 08-21-2007, 02:08 AM
venkatesan.N venkatesan.N is offline
D-Web Trainee
 
Join Date: Jul 2007
Posts: 11
venkatesan.N is on a distinguished road
Thumbs up Using .htaccess files in apache

With the help of .htaccess files we can override the apache web servers config rules. These file types will hidden by default.It has no filename just only extension as ".htaccess". Content will be similar to text file. To change the httpd config files by users they can place .htaccess files per-directory to override the existing rules.So that while we are hitting url in browser, the web servers will search for .htaccess files first on every hit.

Advantages of .htaccess files
  • We can customize error documents.
  • We can add special MIME types
  • We can easily set environment variables
  • We can Redirect URLs from one to another
  • We can Rewrite from one URL into another
  • We can Restrict documents or files to specific people.

Thanks
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-28-2007, 09:44 AM
sivaramakrishnan sivaramakrishnan is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 74
sivaramakrishnan is on a distinguished road
Thumbs up Re: Using .htaccess files in apache

ErrorDocuments

In order to specify your own ErrorDocuments, you need to be slightly familiar with the server returned error codes. (List to the right). You do not need to specify error pages for all of these, in fact you shouldn't. An ErrorDocument for code 200 would cause an infinite loop, whenever a page was found...this would not be good.

You will probably want to create an error document for codes 404 and 500, at the least 404 since this would give you a chance to handle requests for pages not found. 500 would help you out with internal server errors in any scripts you have running. You may also want to consider ErrorDocuments for 401 - Authorization Required (as in when somebody tries to enter a protected area of your site without the proper credentials), 403 - Forbidden (as in when a file with permissions not allowing it to be accessed by the user is requested) and 400 - Bad Request, which is one of those generic kind of errors that people get to by doing some weird stuff with your URL or scripts.

In order to specify your own customized error documents, you simply need to add the following command, on one line, within your htaccess file:

ErrorDocument code /directory/filename.ext
or
ErrorDocument 404 /errors/notfound.html
This would cause any error code resulting in 404 to be forward to yoursite.com/errors/notfound.html

Likewise with:
ErrorDocument 500 /errors/internalerror.html

You can name the pages anything you want (I'd recommend something that would prevent you from forgetting what the page is being used for), and you can place the error pages anywhere you want within your site, so long as they are web-accessible (through a URL). The initial slash in the directory location represents the root directory of your site, that being where your default page for your first-level domain is located. I typically prefer to keep them in a separate directory for maintenance purposes and in order to better control spiders indexing them through a ROBOTS.TXT file, but it is entirely up to you.

If you were to use an error document handler for each of the error codes I mentioned, the htaccess file would look like the following (note each command is on its own line):

ErrorDocument 400 /errors/badrequest.html
ErrorDocument 401 /errors/authreqd.html
ErrorDocument 403 /errors/forbid.html
ErrorDocument 404 /errors/notfound.html
ErrorDocument 500 /errors/serverr.html

You can specify a full URL rather than a virtual URL in the ErrorDocument string (http://yoursite.com/errors/notfound.html vs. /errors/notfound.html). But this is not the preferred method by the server's happiness standards.

You can also specify HTML, believe it or not!

ErrorDocument 401 "<body bgcolor=#ffffff><h1>You have
to actually <b>BE</b> a <a href="#">member</A> to view
this page, Colonel!

The only time I use that HTML option is if I am feeling particularly saucy, since you can have so much more control over the error pages when used in conjunction with xSSI or CGI or both. Also note that the ErrorDocument starts with a " just before the HTML starts, but does not end with one...it shouldn't end with one and if you do use that option, keep it that way. And again, that should all be on one line, no naughty word wrapping!

Regards
sivaraman,
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-28-2007, 09:46 AM
sivaramakrishnan sivaramakrishnan is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 74
sivaramakrishnan is on a distinguished road
Default Re: Using .htaccess files in apache

Adding MIME Types

What if your server wasn't set up to deliver certain file types properly? A common occurrence with MP3 or even SWF files. Simple enough to fix:

AddType application/x-shockwave-flash swf

AddType is specifying that you are adding a MIME type. The application string is the actual parameter of the MIME you are adding, and the final little bit is the default extension for the MIME type you just added, in our example this is swf for ShockWave File.

By the way, here's a neat little trick that few know about, but you get to be part of the club since JavaScript Kit loves you: To force a file to be downloaded, via the Save As browser feature, you can simply set a MIME type to application/octet-stream and that immediately prompts you for the download

Regards
sivaraman,
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-28-2007, 09:52 AM
sivaramakrishnan sivaramakrishnan is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 74
sivaramakrishnan is on a distinguished road
Post Re: Using .htaccess files in apache

Setting Environment Variables

When a script is called it receives a lot of environment variables, as we have seen. It may be that you want to pass some of your own. There are two directives to do this: SetEnv and PassEnv.

SetEnv

SetEnv variable value
Server config, virtual hosts

This directive sets an environment variable that is then passed to CGI scripts. We can invent our own environment variables and give them values. For instance, we might have several virtual hosts on the same machine that use the same script. To distinguish which virtual host called the script (in a more abstract way than using the HTTP_HOST environment variable), we could make up our own environment variable VHOST:

<VirtualHost host1>
SetEnv VHOST customers
...
</VirtualHost>
<VirtualHost host2>
SetEnv VHOST salesmen
...
</VirtualHost>

UnsetEnv

UnsetEnv variable variable ...
Server config, virtual hosts

PassEnv

PassEnv

This directive passes an environment variable to CGI scripts from the environment that was in force when Apache was started.[41] The script might need to know the operating system, so you could use the following:

Note that when Apache is started during the system boot, the environment can be surprisingly sparse.

PassEnv OSTYPE

This variation assumes that your operating system sets OSTYPE, which is by no means a foregone conclusion

Regards
sivaraman,
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-28-2007, 09:57 AM
sivaramakrishnan sivaramakrishnan is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 74
sivaramakrishnan is on a distinguished road
Post Re: Using .htaccess files in apache

Guys,
Rewrite Rule

Refer:URL Rewriting | redirecting URLs with Apache’s mod_rewrite || HTMLSource ]

Regards,
sivaraman,
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 08-29-2007, 09:11 AM
venkatesan.N venkatesan.N is offline
D-Web Trainee
 
Join Date: Jul 2007
Posts: 11
venkatesan.N is on a distinguished road
Thumbs up Re: Using .htaccess files in apache

Hi Sivaraman

Can u explain with an example and syntax, how to redirect and rewrite rules?

Thanks
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 08-31-2007, 01:09 AM
sivaramakrishnan sivaramakrishnan is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 74
sivaramakrishnan is on a distinguished road
Thumbs up Re: Using .htaccess files in apache

Guys,

Using Rewrite Rule to redirect URL's using .htaccess file

Ex
RewriteEngine On
RewriteRule http://example/test.php http://example/index.php [R]

URL:
http://example/test.php---->original page to redirect to http://example/index.php

Regards,
sivaraman,
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Implement SVN in APACHE vadivelanvaidyanathan Server Management 1 08-28-2007 07:18 AM
How to make use of .htaccess file to password protect a directory from users bluesky PHP Programming 1 07-17-2007 08:49 AM
Htaccess www to non www vadivelanvaidyanathan Search Engine Optimization 0 06-30-2007 01:13 AM
What is Apache? nhoj Server Management 3 05-11-2007 03:56 AM
How do I use .htaccess to redirect LennyP Server Management 2 02-26-2007 03:04 AM


All times are GMT -7. The time now is 02:20 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0