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.

Canonization

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

A lot of webmasters suffer from not very good search engine ranking because there are several versions of the home page URL, for example:

  Http://www.site.com 
Http://site.com
Http://www.site.com/index.html
Http://site.com/index.html

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

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

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

This code will allow you to redirect all possible home page options to http://www.site.com.

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 should 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 http://www.site.com/404.php
# Error handling 403 - Access denied
ErrorDocument 403 http://www.site.com/403.php

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 index page of the directory or another suitable page. For example, www.site.com/images/ can be redirected to www.site.com, and www.site.com/forum/ can be redirected to www.site.com/forum/index.php

Redirecting Pages

A great example of using htaccess for redirecting one page to another:

  Redirect 301 /old-page.php http://www.site.com/new-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 http://feeds.feedburner.com/blog-feed.

Redirection should be applied to all visitors except the Feedburner:

  RewriteCond% {HTTP_USER_AGENT}! FeedBurner 
RewriteRule ^ your-feed \ .xml $ http://feeds.feedburner.com/blog-feed [R, L]

Improved hotlink protection

If you want to prevent other websites from directly linking to 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 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 www.site.com" 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 www.site.com/product.php?id=123 into static and user-friendly views of www.site.com/product/123.

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

Another example that leads the URL: www.yoursite.com/script.php?product=123 to the form www.yoursite.com/cat/product/123/

  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 http://www.site.com/index.php?source=seoblog, just to know where the traffic comes from. This causes the appearance of duplicate content 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 you in kament ...

On most webhosts, two or more versions of PHP are installed. Usually the 4th 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 brave 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 mandatory, 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)

* Yoursitename.ru 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 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 the content from the sites steal, this 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) $ http://yoursitename.ru/images/imagename.jpg [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 errors during debugging

  Php_flag display_errors on 

Configuring a redirect using .htaccess

Configuring 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 http://yoursitename.ru/newpage.html 

There is another such variant:
  Redirect permanent /oldpage.html http://yoursitename.ru/newpage.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 yoursitename.ru on www.yoursitename.ru.

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

3. 302 redirect
If you work 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 on 
RewriteCond% {REQUEST_URI}! /informpage.html$
RewriteCond% {REMOTE_HOST}! ^ 12 \ .345 \ .678 \ .90
RewriteRule $ http://yoursitename.ru/informpage.html [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 http://www.yoursitename.ru 
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}

Prohibition / locking with .htaccess

Prohibition / locking with .htaccess

1. The ban on listing the directory
Used in the absence of an index file, if you want to prevent 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 a specific folder only

  <Directory / root-access /> 
Order allow, deny
# All but ...
Allow from all
Deny from 198.69.
</ Directory>
Permit access only from certain IP 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 198.69.144.98 198.69.144.94 198.69.144.92
</ Directory>
It is also possible to specify in the same list both the host name and the IP address:
  # 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 robot.fromx.com 148.145.21.14
</ 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 .tvoe.tv
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 tvoe.tv
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 198.69.132.24
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 194.111.70.48 194.78.47.128
In case you need to close access by mask then use the incomplete address:
  # Incomplete address 
198.69.
# Full addresses corresponding to the first
198.69.132.24
198.69.136.89
198.69.1.8
Close access by mask:
  # The ban 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 setting the address of the page. 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 looks like this: yoursitename.ru/?p=xxxx, where xxxx is the id of the article.

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

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

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