This page has been robot translated, sorry for typos if any. Original content here.

Guide to .htaccess and mod_rewrite

On this topic:

Файл-конфигуратор Apache-серверов .htaccess

Quick Start Guide for htaccess and mod_rewrite

The use of Apache "chips" mod_rewrite and htaccess is considered something unattainable among the many disappointed webmasters, however this is one of the most difficult tasks that almost all web developers meet.

I want to offer you a simple solution instead of searching for more complex ones in order to understand how mod_rewrite and htaccess work . Most of the examples of use described in this article can be simply copied and uploaded to your web server.

In order not to repeat the same to hundreds of other webmasters, I decided to just create this page-tip for general use.

Again, htaccess is a configuration file that allows you to manage the Apache web server, mod_rewrite is a rewriting engine with which the web server can modify the URLs when they are downloaded.

The htaccess file is a text file that has the name .htaccess . You can even say that this is a file consisting only of an extension, because it does not contain the nominal part (before the "dot" symbol). Usually this file is located in the main root directory of your server, but you can also create separate htaccess files for different directories on your site.


The simplest example is to make sure that the site does not contain canonical errors for the home page.

Many webmasters suffer from a not very good ranking in the search engines because there are several versions of the URL of the home page, for example:

These pages look like different URLs, even though they point to the same content. Google will ultimately decide which version is more correct, but it can take a while, and during this time you can have unnecessary problems.

To solve this problem, simply add the following lines to your htaccess file:

  Options + FollowSymLinks 
RewriteEngine on
RewriteCond% {HTTP_HOST} ^
RewriteRule (. *) Http://$1 [R = 301, L]
RewriteCond% {THE_REQUEST} ^ [AZ] {3,9} \ / index \ .html \ HTTP /
RewriteRule ^ index \ .html $ [R = 301, L]

This code will allow you to redirect all possible home page options to

Converting HTML files to PHP

Sometimes you need to use PHP code on the HTML pages of a static site. Instead of redirecting all your HTML pages to their equivalent PHP versions, you just need to "tell" your server that it needs to parse HTML files as PHP.

  AddHandler application / x-httpd-php .html 

This code works with any files, so if you want to create dynamic XML or ASP files that behave like PHP files, you just need to make the following changes:

  AddHandler application / x-httpd-php .xml 
AddHandler application / x-httpd-php .asp

Error pages

Custom error pages can be easily customized, for example in CPanel (or DirectAdmin), but if you want to configure the error page via an htaccess file, add the following line to it:

  # Error handling 404 - File not found 
ErrorDocument 404
# Error handling 403 - Access denied
ErrorDocument 403

Indexing of directories

To prevent Google from indexing directories, you must define an index page for the required directories. This is not required on some servers.

  DirectoryIndex index.php3 

I prefer to redirect to the directory index page or another suitable page. For example, can be redirected to, and can be redirected to

Redirecting pages

A great example of using htaccess to redirect one page to another:

  redirect 301 /old-page.php 

Redirecting your feed to Feedburner

If you want to switch your feed to the Feedburner service, you will need to redirect your current feed to the new address

Redirection should be applied to all visitors except the Feedburner:

  RewriteCond% {HTTP_USER_AGENT}! FeedBurner 
RewriteRule ^ your-feed \ .xml $ [R, L]

Improved hotlink protection

If you want to prevent other websites from directly referencing images located on your site, but you can index Google search engines Google, Yahoo and MSN, you need to use the following code:

  RewriteEngine on 
RewriteCond% {HTTP_REFERER}.
RewriteCond% {HTTP_REFERER}! ^ Http: // ([^.] + \.)? Site \. [NC]
RewriteCond% {HTTP_REFERER}! Google \. [NC]
RewriteCond% {HTTP_REFERER}! Search \? Q = cache [NC]
RewriteCond% {HTTP_REFERER}! Msn \. [NC]
RewriteCond% {HTTP_REFERER}! Yahoo \. [NC]
RewriteCond% {REQUEST_URI}! ^ / Hotlinker \ .gif $
RewriteRule \. (Gif | jpg | png) $ /hotlinker.gif [NC, L]

Image hotlinker.gif is an image that you created. I suggest using something like "This image is on" and your logo.

Personally, I allow hotlinks, but I use a certain solution that uses the Google Images service and hotlink sites to get links to my site .

Creating CNC links using mod_rewrite

The Apache rewrite module is mainly used to turn dynamic URLs of the form into static and user-friendly views of

  RewriteEngine on 
RewriteRule ^ product /([^/\.]+)/?$ product.php? Id = $ 1 [L]

Another example that leads the URL: to the form

  RewriteRule cat /(.*)/(.*)/$ /script.php?$1=$2 

Deleting query strings

Some sites link to yours by adding a query string. For example, you can refer to your site using this link, just to know where the traffic comes from. This causes the duplicate content to appear for your site, so you need to configure the redirection to your home page:

  RewriteCond% {QUERY_STRING} ^ source = RewriteRule (. *) / $ 1?  [R = 301, L] 

That's all for now. If there is, what to add, I ask in kament ...

Most hostings have two or more versions of PHP installed. Usually the fourth is the default. However, in order for your PHP scripts to be processed in version 5, you can register in htaccess:

  AddType application / x-httpd-php5 .php 

Or you can add a .php5 extension to each file instead of just .php.
This is in case you are too lazy to redact htaccess.

Examples of configuring the .htaccess file

While some of our valiant seo "gurus" translate foreign articles, without bothering to install a link to the original article, I'll give you a selection of codes for configuring .htaccess.

Almost all items are required, except

  • redirects and locks - they are configured as needed
  • as well as the settings of the CNC (Human FunLink) - as a rule, you can do CMS

How to create .htaccess?

  1. open notepad
  2. write the code (you can and then do it :)
  3. save the document with the name .htaccess
  4. fill in the root of the site (or in the folder on the server where we will make the settings)

* in the examples replace with the address of your domain

Default htaccess settings

Default htaccess settings

1. Set the default home page

  DirectoryIndex pagename.html 

2. Defining error pages (error) in .htaccess

  ErrorDocument 403 /403.php 
ErrorDocument 404 /404.php
ErrorDocument 500 / 500.php
Accordingly, the address and file name can be different, for example /error/page404.html

3. Protection from hotlinks
The fact that content from sites steal is not news for a long time. But it happens that the rod together with the pictures. Here in order to avoid the load on the server and the inflation of traffic, we write:

  RewriteEngine On 
RewriteCond% {HTTP_REFERER}! ^ $

# domains that can link to pictures from your site
RewriteCond% {HTTP_REFERER}! ^ Http: // ([-a-z0-9] \.)? Yoursitename \ .ru [NC]

# do not show images when hotlinks
RewriteRule \. (Gif | jpe? G | png) $ - [F, NC, L]
If you want a certain picture to appear on a strange site instead of 403 errors, the last line should be replaced with:
  RewriteRule \. (Jpg | png | gif) $ [NC, R, L] 

4. Force the file to be saved instead of opening
If you want visitors to save the file online instead of viewing it, then in .htaccess we write:

  AddType application / octet-stream .doc .mov .avi .pdf .xls .mp4 

5. Caching files
In WordPress there is a special plugin for this: wp-super-cache. If you use a CMS that does not support caching, you can make the following settings in .htaccess

 # caching html and htm per day 
<FilesMatch ". (Html ​​| htm) $">

Header set Cache-Control "max-age = 43200"
</ FilesMatch>

# caching css, javascript and txt files for the week
<FilesMatch ". (Js | css | txt) $">

Header set Cache-Control "max-age = 604800"
</ FilesMatch>

# Flash caching and images for a month
<FilesMatch ". (Flv | swf | ico | gif | jpg | jpeg | png) $">

Header set Cache-Control "max-age = 2592000"
</ FilesMatch>

# prohibiting caching of scripts
<FilesMatch "\. (Pl | php | cgi | spl | scgi | fcgi) $">

Header unset Cache-Control
</ FilesMatch>
Parameter max-age is given in seconds.

6. Enabling debugging errors

  php_flag display_errors on 

Setting up a redirect using .htaccess

Setting up a redirect using .htaccess

1. 301 redirects
The optimal solution for redirecting visitors from old addresses to new ones, if the structure of the site has changed over time, and the old URLs could have stayed somewhere.

  Redirect 301 /oldpage.html 

There is another such variant:
  Redirect permanent /oldpage.html 
To be honest, I can not explain the fundamental difference between the two. Maybe one of the readers will tell.

2. Bonding domains with www and without www
Redirect from on

  RewriteEngine On 
RewriteBase /
RewriteCond% {HTTP_HOST}! ^ $ [NC]
RewriteRule ^ (. *) $ Http://$1 [L, R = 301]
Reverse redirection
  RewriteEngine On 
RewriteBase /
RewriteCond% {HTTP_HOST}! ^ $ [NC]
RewriteRule ^ (. *) $ Http://$1 [L, R = 301]

3. 302 redirects
If you are working on the site, you can temporarily send visitors to the desired page (for example, with a description of when the work is planned to be completed and how you can be contacted).

  RewriteEngine on 
RewriteCond% {REQUEST_URI}! /informpage.html$
RewriteCond% {REMOTE_HOST}! ^ 12 \ .345 \ .678 \ .90
RewriteRule $ [R = 302, L]
The site will be viewed only from your ip-address - 12.345.678.90

4. Redirecting to another site
Forwarding all visitors to another site, in addition to the specified ip-addresses

  ErrorDocument 403 
Order deny, allow
Deny from all
Allow from 12.345.678.90
Allow from 12.345.678.92

5. Redirecting to a secure https connection

  RewriteEngine On 
RewriteCond% {HTTPS}! On
RewriteRule (. *) Https: //% {HTTP_HOST}% {REQUEST_URI}

Disable / block using .htaccess

Disable / block using .htaccess

1. The ban on listing the directory
Used in the absence of an index file, if you want to prohibit viewing the contents of the folder (for example, for images):

  Options All -Indexes 
Reverse code, in the sense of permission
  Options + Indexes 

2. Blocking access to the file
For example, it could be your .htaccess

  <Files .htaccess> 
order allow, deny
deny from all
</ Files>

3. Block access to the directory
Block access to only a specific folder

  <Directory / root-access /> 
Order allow, deny
# All but ...
allow from all
Deny from 198.69.
</ Directory>
Permit access only from certain IPs to a specific folder (addresses are written with a space):
  # Specify the folder to which we deny access by host name and its IP 
<Directory / root-access />
Order deny, allow
deny from all
# Addresses are written with a space
Allow from
</ Directory>
Also, it is possible to specify both a host name and an IP address in one list:
  # Specify the folder to which we deny access by name and IP 
<Directory / root-access />
Order allow, deny
# To all, ...
allow from all
# ... except the host name and its IP
Deny from
</ Directory>

4. Blocking by ip
That spammers do not bother

  <Limit GET> 
Order Deny, Allow
Deny from 12.345.678.90
Deny from 90.876.543.
Deny from
Allow from all
</ Limit>
Will be blocked:
  • user with ip 12.345.678.90
  • all visitors with ip, falling in the range from 90.876.543.0 to 90.876.543.255
  • and all visitors from the provider
To prevent access to the site folder, the allow and deny directives are used.
Deny access from a specific IP address
  # Prohibition from 16:18 on 31-03-2011 
Order allow, deny
allow from all
Deny from
Close access to ALL except certain IPs
  Order deny, allow 
deny from all
# List of IPs through the space from which access is allowed
Allow from
In case you need to close access by mask then use the incomplete address:
  # incomplete address 
# full addresses corresponding to the first
Close access by mask:
  # Prohibition from 06:18 on 31-03-2011 
Order allow, deny
allow from all
Deny from 198.69.

CNC settings with .htaccess

In general, if you are working with WordPress, then you can set up a "friendly" url in the Settings »Permanent links (CNC) section. Many CMS also support the setting of the page address. Therefore, if possible, first try using the engine's functionality, and then start configuring htaccess.

CNC settings with .htaccess

1. How to remove from the address? P =
By default the address of the page is as follows:, where xxxx is the id of the article.

  RewriteCond% {REQUEST_URI} ^ / ([0-9] +) $ 
RewriteRule. * Http:// [R = 301, L]

2. How to remove from the address category
By analogy with the past way we write

  RewriteRule ^ category /(.+)$$1 [R = 301, L]