FAQ: How do I use .htaccess?

Locked
User avatar
Elpie
Joomla! Guru
Joomla! Guru
Posts: 903
Joined: Wed Aug 17, 2005 11:26 pm
Contact:

FAQ: How do I use .htaccess?

Post by Elpie » Sun Jul 23, 2006 1:29 am

.htaccess files are "distributed configuration files" and provide a way to make configuration changes on a per-directory basis. What you can put in .htaccess files is determined by the AllowOverride directive which is set up by the host. This directive specifies, in categories, what directives will be honored if they are found in a .htaccess file.
So, whether the directives you want to use in an .htaccess file work depends very much on how your host has set up the server. Most hosts allow some .htaccess directives.

The directives used for our SEF URL rewriting is generally allowed, however not all hosts will permit php_flags to be set, for example, to change register_globals settings.

In general, you should never use .htaccess files unless you don't have access to the main server configuration file. Unless you are running your own server you won't have access to this.

.htaccess files cause a performance hit. When a host has AllowOverride set to allow the use of .htaccess files Apache will look in every directory for .htaccess files - it looks whether you use .htaccess files or not. The .htaccess is also loaded every single time a call is made to your site. Added to this, Apache must look at every .htaccess file that exists in directories higher than the one it is calling.

The way this works is basically this, eg:
You place an .htaccess file in your site's root, then another one in a component directory, when that component is called Apache has to look at the component directory .htaccess and also at the one higher up, in the root.
Directives are applied in the order in which they are found by Apache. Therefore, a .htaccess file in a particular directory may override directives found in .htaccess files found higher up in the directory tree. And those, in turn, may have overridden directives found yet higher up, or in the main server configuration file itself. Apache has to look at any .htaccess in the particular directory that has been called and all the others higher up so it can determine which directives to use when it processes the request to call the page on your site.

So, when you see me write in posts that every directive added to a .htaccess file adds to server load, you will understand why I say this. If you have a large .htaccess file with a lot of directives and you put this in your site root, you are likely to see a large hit on performance. Not only are there increased file-system accesses, but Apache has to also process whatever the directives are telling it to do.

With me so far?
Let's talk about the hottest topic of the month - using .htaccess to turn off register_globals.

Whether you want register_globals ON or OFF in your webspace, changing hosts should not be necessary. If a host sets PHP to run register_globals ON, this can often be easily overridden with a simple change to your site root .htaccess. Just use either of these, depending whether you want them on or off. Note: this only works if your host has enabled AllowOverrides for your account.

Code: Select all

php_flag register_globals on 
php_flag register_globals off


If this does not work for you, you can also use :

Code: Select all

php_value register_globals 0
php_value register_globals 1


php_value register_globals 1 (ON) or 0 (OFF) - Apache recognises it as the same as the directives I have used above. If you do this, your php.info should then show that register_globals is either on or off, depending on which option you chose.

Now, here is the tricky part. These directives ONLY work on sites that have PHP running under Apache, as an Apache module, not as CGI. PHPSuExec runs under CGI and setting the register_globals in .htaccess will not work.

People running Apache 1.x can use the directives above as is, but people running under Apache 2.x may need to use the directory structure. (This depends again on how your host has set this up).
By "directory structure" I mean and . These are used to enclose a group of directives which will apply only to the named directory and sub-directories of that directory. You must make sure that you use the correct syntax for your .
This is the syntax for Apache 1.3 (search in the manual for your version of Apache) http://httpd.apache.org/docs/1.3/mod...html#directory

php_flag does not work with Apache 2. Neither does writing the words "on" or "off" — you must use 0 (off) or 1 (on). So, for Apache 2.x, you use this directive to turn register_globals OFF:

Code: Select all

php_value register_globals 0


I get a 500 server error.
If you get server errors, check your site's error logs. The logs are likely to show you one of two things - either the directive you have used is not permitted by the way the host has set the server up, or you have a syntax error in the directive.

How do I know if it is working?
This is a question everyone should ask but few ever do. Do NOT assume your .htaccess file is working in the way you expect. If you have turned off register_globals by using an .htaccess directive, check your php.info file and make sure it is reporting that register_globals is off.

If you are an experienced user you can always put some garbage in your .htaccess and watch for a server error. If you dont get one, you know your .htaccess is being ignored. Don't do this if you are not completely confident that you know how to get back from such a situation as .htaccess overrides server configuration for your server space and you could find yourself completely locked out of your account (and facing a rather angry host).

If your host has not set an AllowOverride for your server space, your .htaccess will be completely ignored and you won't get a server error message telling you about it. If you think your .htaccess is being ignored, you need to contact your host and ask if they have set AllowOverride, and if so, what is allowed.

.htaccess is a complex area and I have only touched on some of the issues. If you have any questions or suggestions, please post these in the FAQ discussion area.

If you want to redirect all traffic to www, add this to your .htaccess file:


Code: Select all

Options +FollowSymLinks 
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]


Remember to change the "domain.com" to your domain! If you are using SEF, you probably won't have to add the

Code: Select all

Options +FollowSymLinks
 RewriteEngine on



---------------------------------------------------------------------------------------------------
Originally published on Mambo Guru. Reproduced with permission. If you want to reproduce this on other sites please contact me for permission.

Discussion located here: http://forum.joomla.org/index.php/topic,88828.0.html
Last edited by mcsmom on Sun Oct 22, 2006 1:07 am, edited 1 time in total.
For Mambo assistance: http://forum.mambo-foundation.org
Open Source Research & Best Practice: http://osprojects.info

Locked

Return to “FAQs not moved”