.htaccess

Web-master / Your website / Web Help

.htaccess

Attention!
I hope, you will guess to make a copy of the existing file .htaccess, and only then will produce your experiments.
If you do not like the result of your work, just put the old one back in place.

Let it be an auxiliary file, the overloaded can slow down the server.

Generator .htaccess

.htaccess

The .htaccess file contains the user settings of the web server and is analyzed for each request to the server.
On a virtual hosting is most often used for:
  • directory passwords,
  • page forwarding,
  • Customize your own error pages,
  • encoding definitions,
  • prohibitions and permits,
  • and others.
This is a flexible configuration of the Apache web server.
With it, you can override many directives from the httpd.conf file (the main Apache server configuration file).
If the .htaccess file is located in the root directory of the server, then its actions extend to the entire server, except for those folders where there is another .htaccess file.

Editor

You can create .htaccess files in Notepad or in the built-in editor Far, WindowsCommander, TotalCommander.
I edit it with Notepad ++ or Akelpad, which I use to write this site.

Encoding

How to expose the Russian encoding.

AddDefaultCharset utf-8

Determine the encoding for certain file types

AddType "text/html; charset=koi8-r" .html .htm .shtm

Determining the encoding for uploaded files

CharsetSourceEnc utf-8

Disable transcoding

If the binary files (pictures, archives) are not loaded (beat) during the loading by the script, create the .htaccess file and put its folder where you want to disable the function, in it write the line:

CharsetRecodeMultipartForms off

<FilesMatch "\.(htm|html|css|js)$">
AddDefaultCharset UTF-8
</FilesMatch>


Assign a start page.

DirectoryIndex index.html

You can specify multiple pages.

DirectoryIndex index.html index.php index.shtml map.html

When a directory is requested, they will be searched in the order listed in the DirectoryIndex directive. If the index.html file is not found, the index.php file will be searched, etc.


Allow/Deny

Allow access from a specific IP.

order allow deny
deny from all
allow from 000.000.000.000

Deny access from a specific IP.

order allow deny
allow from all
deny from 000.000.000.000

Prohibit view to everyone.

deny from all

Enable view to everyone.

allow from all

Deny access to a specific file.

<Files config.php>
deny from all
</Files>

Deny access to files with a specific extension.

<Files "*.conf">
deny from all
</Files>

Deny access to multiple extensions.

<Files "\.(inc|conf|cfg)$">
deny from all
</Files>

? - Any single character
* - any characters sequence, excluding the character/(slash).

Fine-tune access.

There is a directory a1 and there are two sub-folders a2, a3 in it, 2 user levels are entered. 1 group has access only to a1 and a2, 2 to all three directories. It is necessary to authenticate only once - when accessing a1, but at the same time respecting the access rights to a2 and a3.
Nickname and password are requested only when logging in to a1 - if the user has access to the a2 password is no longer requested. If there is no access to a3, a "enter a password".

www.site.ru/a1
www.site.ru/a1/a2
www.site.ru/a1/a3
a1 - common and at the same time closed. A2 and a3 only for individuals.

.htaccess file for the directory a1:

AuthName "Input password"
AuthType Basic
AuthUserFile "/pub/home/login/htdocs/clousearea/.htpasswd"
<Files *.*>
require valid-user
</Files>


.htaccess file for the directory a2:

AuthName "Input password"
AuthType Basic
AuthUserFile "/pub/home/login/htdocs/clousearea/.htpasswd"
<Files *.*>
require user user1 user2 user3
</Files *.*>

file .htaccess for catalog a3:

AuthName "Input password"
AuthType Basic
AuthUserFile "/pub/home/ABC/htdocs/clousearea/.htpasswd"
<Files *.*>
require user user1 user4 user5
</Files *.*>


Redirect

Paths to files and directories should be specified from the root of the server, for example, /pub/home/server1/html/
Domain names must contain the following protocols (http://, http://, ftp://)

If you do not know the path from the root of the server, you can find it by asking the server administrator, or you can see for yourself by running the PHP function - phpinfo () to the site. It will display the PHP configuration in the form of purple tables. In them you need to find the variable doc_root and see its value - this will be the path from the server root to your main directory.

Redirection only when requesting certain pages.

Redirect/location/from/root/file.ext http://www.serv.ru/new/file/location.xyz

/location/from/root/file.ext - this is the path to the root file (to the desired file).

www.serv.ru/new/file/location.xyz is the place where the user will enter by making a request for a file file.txt.

If the file is in the root directory, then instead of location/from/root/you can put:

/oldfile.html

Well, if the file is in a subdirectory, it will look like this:

/old/oldfile.html

Redirect to a folder (e.g. folder oldfolder).

Redirect/oldfolder http://www.serv.ru/newfolder

enter www.oldserv.ru/oldfolder/images/smile.gif,
go to www.serv.ru/newfolder/images/smile.gif.

Global redirect (redirect) to another address. Redirect/http://www.newsite.ru

Redirect (redirect) only visitors with a specific IP address.

SetEnvIf REMOTE_ADDR 192.145.121.1 REDIR="redir"
RewriteCond %{REDIR} redir
RewriteRule ^/$/only_for_you.html


Remove the WWW

The matter is that people can type the address of your site habitually with WWW. Or saving time without WWW.
For Yandex and Google http://your-site.com and http://www.your-site.com - different sites. And you, as a consequence, will have an underestimated citation index.

This is solved by the following code.

If you want your address to be always with WWW. (More familiar), then the code:

RewriteEngine on
Options +FollowSymlinks
RewriteBase/
RewriteCond %{HTTP_HOST} ^your-site.com$
RewriteRule ^(.*)$ http://www.your-site.com/$1 [R=301,L]

Instead of YOUR-SITE.COM we put your address.


Password

Password protection.

AuthName "Section Name" AuthType Basic AuthUserFile/full/path/to/.htpasswd Require valid-user

You need to change for your site. For example, change Section Name to its name (for example - Members area).

/full/path/to/.htpasswd - The full path to the .htapasswd file (more on this later).

If you do not know Full path, please contact your system administrator.

The .htpasswd file

To protect the directory with a password, you must create a file containing logins and passwords that will be allowed to visit the site. And write it into the .htpasswd file.

Create usernames and passwords.

We enter usernames and passwords trace. Way:

username:password

Password encrypted version of the password.
To encrypt a .htpasswd file, you will need to use a ready-made script, or write it yourself. If you need several users, then write them line by line.

Once you enter the password-protected directory, you will pop up a pop-up window for entering your login and password. And you can simply enter in the address field:

username:password@www.serv.ru/directory


Accelerate the site with gzip

This compression reduces the size of your files, and as a result reduces the traffic of a visitor, your site is more quickly opened.

But this is suitable only for small and medium sites, because you increase the load on the server.

AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml
application/xhtml+xml text/JavaScript text/css application/x-JavaScript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html


How to get Apache to handle SSI directives?

SSI allows you to "collect" a page from pieces. In one piece you have the code of the menu, in the other code at the top of the page, in the third one - the bottom one. A visitor sees a normal page that consists of the code that goes into your pieces. Required installation in httpd.conf is required:
In the block beginning with and ending with in the Options Indexes line, add Includes.
and then, in the .htaccess file Write:

AddHandler server-parsed .shtml .shtm .html .htm

To the user did not see the list of files in the folder, if there is no index file in it, add the line:

Options -Indexes

Running php code in files with a different extension.

Sometimes it is useful to "deceive" the visitor by giving him his php-scripts or other files, like html files. It is actually used for indexing by the search system Rambler php-scripts.

RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html .phtml

Note, since the files with the extension .html and .htm are associated that they are files with hypertext markup, then we remove this mapping first and we add the interpreter for processing such files with the php.

With a large server attendance, such tricks can cause brakes. Ask the admin.

Error 404 ...

If there is no file reporting the error, then the visitor will see the contents of your site, as in the explorer. Perhaps you've seen a picture similar to the one below:


You can create a special file, where there will be links to your site, not to the hoster. Call it 404.html and put it in the root directory.
The size of the page with an error should be more than 511 bytes, otherwise Internet Explorer can show its own page describing the error.

ErrorDocument 404/404.html
ErrorDocument 403/403.html
ErrorDocument 401/401.html
ErrorDocument 500/500.html

You can specify another file that will automatically open when an error occurs.
Site map - is an ideal file for error 404 error handling (file not found).

ErrorDocument 404/map.html

register_globals

If your mail form does not work, register_globals may not be enabled, then add the line:

php_flag register_globals on

Or php_flag register_globals off if you want to disable.


FORCED PURCHASE OF CERTAIN FILES

You can download some files (MP3, EPS, XLS ...) for downloading, and not allow the browser to decide what to do. That is, make a decision for it.
This code snippet will force the download and .xls files from the .eps file from your server.

<Files *.xls>
   ForceType application/octet-stream
   Header set Content-Disposition attachment
</Files>
<Files *.eps>
   ForceType application/octet-stream
   Header set Content-Disposition attachment
</Files>


HOTLINK PROTECTION

Hotlink - when they put a direct link to your content from another site.
Often such a situation is undesirable, you do not want to pay for the traffic of another site, and to avoid it you use:

SetEnvIfNoCase Referer "^http://www.test.com" local_ref=1
SetEnvIfNoCase Referer "^http://test.com" local_ref=1
<FilesMatch ".(gif|jpg)>
Order Allow,Deny
Allow from env=local_ref
</FilesMatch>

Another variant:

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://www.quux-corp.de/~quux/.*$ [NC]
RewriteRule .*\.(gif|jpg)$ - [F]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !.*/foo-with-gif\.html$
RewriteRule ^inlined-in-foo\.(gif|jpg)$ - [F]

More option:

RewriteEngine On
#Replace ?mysite\.com/with your blog url
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/[NC]
RewriteCond %{HTTP_REFERER} !^$
#Replace/images/nohotlink.jpg with your "don't hotlink" image url
RewriteRule .*\.(jpe?g|gif|bmp|png)$/images/nohotlink.jpg [L]