File use .htaccess in optimisation and management of a site
Date: 22 January 2009
File.htaccess use for change of various options of a web server in relation to a separate directory and to all directories enclosed in it. It is very useful instrument for the web designer by internal optimisation of a site as allows to avoid set of problems. And them happens much. Here only some of file possibilities.htaccess:
1. A redirection on any page at processing of errors on a server (for example, an error 401, 403, 404, 500); 2. A redirect on any page on ip the user or at inquiry of concrete page (and also on a name mask); 3. A redirect at inquiry of certain files; 4. A redirect on a prefix www (301 redirect); 5. Prohibition of access to a concrete directory for all; 6. The permission of access to a directory under the password (works together with a file.htpasswd) or on defined ip the user; 7. A complete redirect.
The file.htaccess can be created in the Notebook. Fill in a file.htaccess in a root of a necessary directory. Thus it will have an effect on all enclosed directories but only if in them there is no file.htaccess in which there are other instructions.
If in a file it is necessary to indicate ways they are indicated absolute from a server root. You can learn it at hostera or is independent. For this purpose it is necessary to create file PHP with any name and to start it on a server:
<? php phpinfo (); ?>
In the information about established PHP (doc_root, open_basedir, DOCUMENT_ROOT) we search for a complete absolute way.
For correct job of a file.htaccess it is necessary that it has been allowed in a main file of a configuration httpd.conf and, as a rule, all hostings allow it.
And now we will result examples how correctly to use a file.htaccess.
1. A redirection on any page at processing of errors on a server (for example, an error 401, 403, 404, 500). At occurrence (definition) of these errors the user will be redirected on corresponding pages:
2. A redirect on any page on ip the user or at inquiry of concrete page (and also on a name mask). If at the user ip 192.152.37.125 it will be redirected on page user.php:
If certain pages the user will be redirected on other pages are requested:
redirect/secret www.site.com/nosecret
3. A redirect at inquiry of certain files. If the files which expansion is not indicated in a file.htaccess (gif and jpg) the redirection follows are requested:
RewriteEngine On RewriteRule!. (gif|jpg) $ index.php
4. A redirect on a prefix www (301 redirect).
- With site.com on www.site.com: RewriteEngine on Options +FollowSymlinks RewriteBase / RewriteCond % {HTTP_HOST} ^site $ RewriteRule ^ (. *) $ http:// www.site.com / $1 [R=301, L]
6. The permission of access to a directory under the password (works together with a file.htpasswd) or on defined ip the user.
- Under the password: Two files.htaccess and.htpasswd are necessary
In a file.htaccess we write:
AuthName "Danger! Admin!" (The text of a warning inscription) AuthType Basic require valid-user (any who has entered the correct password will enter) AuthUserFile/home/com/www.site.com/admin/.htpasswd (an absolute way to.htpasswd)
In a file.htpasswd the password is underlined. To create this file it is possible with the help htpasswd.exe which is possessed in a directory bin a server. File creation.htpasswd: 1. In the command line pass in a directory bin 2. Enter: htpasswd-cm.htpasswd admin 3. The utility will request input of the password two times, enter it 4. If all have made successfully will see Adding password for user admin 5. The file.htpasswd will be generated in a folder bin.
Then we fill in both files in the necessary directory, and at an input in it the login and the password will be requested. Your directory is protected!
- On ip the user:
order deny, allow deny from all allow from 192.152.37.125
Access to the given directory will be allowed only to the user with ip 192.152.37.125.
7. A complete redirect.
Redirect / http:// www.site.com
All users who have passed to the given site, will be perenapravljatsja on a site www.site.com.
It only some examples of use of a file.htaccess on your site. A range of its use very big, that allows to solve successfully the diversified problems by optimisation of a site and at its management.