.htaccess — Redirects and Web Server Configuration

.htaccess is a configuration file for the Apache web server that allows you to manage server behavior and site settings using various directives, without modifying the main Apache configuration file.

The hosting platform uses Apache version 2.4.

Directives in .htaccess apply to the directory containing the file and all its subdirectories. If you want to change settings for the entire site, place .htaccess in the root directory: ~/your_domain/docs

Be careful when editing .htaccess! When saving in UTF-8, make sure the file does not contain a BOM signature. Use specialized editors instead of Windows Notepad to edit .htaccess and other configuration files. For example, Notepad++.



Examples of Using .htaccess

1. Restricting site access by IP

Deny access from IPs 123.4.5.6 and 123.5.4.3:

Order Allow,Deny
Allow from all
Deny from 123.4.5.6 123.5.4.3

Allow access only from 123.4.5.6 and 123.5.4.3:

Order Allow,Deny
Deny from all
Allow from 123.4.5.6 123.5.4.3

Deny access for everyone:

Deny from all


2. Overriding the site’s home page (default index file)

Make menu.html the home page:

DirectoryIndex menu.html


3. Enabling PHP processing in .html files

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


4. Enabling CGI script execution in docs folder for for files with .cgi, .pl, and .py extenstions.

Place a .htaccess file in the CGI script folder with the content:

AddHandler cgi-script .cgi .pl .py
Options +ExecCGI

The script must have execution rights (+x, e.g., 755). Permissions can be set via the control panel file manager, FTP client, or SSH. Also, in Web Server → Module Management («Веб-сервер» — «Управление модулями»), the CGI module must be enabled.


5. Blocking referrals from external domains

To block access from baddomain.ru to domain.ru:

RewriteEngine on
RewriteCond %{HTTP_REFERER} baddomain\.ru [NC]
RewriteRule .* - [F]

To block multiple domains:

RewriteEngine on
RewriteCond %{HTTP_REFERER} baddomain\.ru [NC,OR]
RewriteCond %{HTTP_REFERER} baddomain2\.ru [NC,OR]
RewriteCond %{HTTP_REFERER} baddomain3\.ru [NC]
RewriteRule .* - [F]


6. Using Cyrillic domains (.рф, .москва, etc.)

Cyrillic characters cannot be used in .htaccess. For redirects with Cyrillic domains, use the punycode representation. You can find the punycode of a domain using a Whois service.

Example: redirect from site.ru to сайт.рф:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.site.ru [NC]
RewriteRule ^(.*)$ http://xn--80aswg.xn--p1ai/$1 [R=301,L]

In this case, visitors may see the punycode domain in the browser address bar. This is not an error.

Redirect Configuration

For Cyrillic domains (.рф, .рус, etc.), you must specify the domain in punycode format.


1. Redirecting domain aliases to the main domain (301 code)

Redirect all requests to domain.ru from any domain alias:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain\.ru$ [NC]
RewriteRule ^(.*)$ http://domain.ru/$1 [L,R=301]

Redirect all requests to www.domain.ru from any domain alias:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.ru$ [NC]
RewriteRule ^(.*)$ http://www.domain.ru/$1 [L,R=301]

It is recommended to place these rules at the very beginning of the .htaccess file.

2. Permanent redirect (301 code)

If you changed a page address, add the following lines to .htaccess so requests from the old URL redirect to the new one:

Redirect 301 /page.html http://www.domain.ru/new_page.html

where:

  • page.html — old page address relative to the site root
  • www.domain.ru — site domain
  • new_page.html — new page address
This rule does not work for redirects from addresses containing a Query String (characters after ?). For requests with a QUERY_STRING, use RewriteCond and RewriteRule.

Example: redirect all requests to /period/?test=123 to domain.ru:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} ^test=123$ [NC]
RewriteRule ^period/$ http://domain.ru/ [L,R=301]


3. Page-by-page redirect to another domain (301 code)

The following rules will redirect all requests from your site to corresponding pages on another site. Example: A request to http://domain.ru/main will be redirected to http://www.newdomain.ru/main.

Redirect 301 / http://www.newdomain.ru/

or:

RewriteEngine On
RewriteRule ^(.*)$ http://newdomain.ru/$1 [R=301,L]


4. Redirect from one page to another (301 code)

Add the following lines to .htaccess:

RRewriteEngine On
RewriteCond %{HTTP_HOST} site.ru/old-page
RewriteRule (.*) http://site.ru/new-page$1 [R=301,L]

where:

  • site.ru/old-page — old page address
  • new-page.html — new page address


5. Redirect for a specific IP (301 code)

Add the following lines to .htaccess:

RewriteEngine On
RewriteCond %{REMOTE_ADDR} 123.123.123.123
RewriteRule ^(.*)$ http://site.ru [R=301,L]

where:

  • 123.123.123.123 — IP address to be redirected
  • site.ru — your website domain


6. Redirect (301 code) from the homepage only, excluding other pages

In the .htaccess file located in the site folder that requires redirection, add:

RewriteEngine On
RewriteRule ^$ https://site.ru [R=301,L]


7. Redirect (301 code) from all site pages except the homepage

In the .htaccess file located in the site folder that requires redirection, add:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ http://site.ru/ [L,R=301]


8. Redirect with “mass glue”

If you need to redirect requests to index.php using .htaccess, enter:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.(php|html|htm)\ HTTP/
RewriteRule ^(.*)index\.(php|html|htm)$ http://site.ru/$1 [R=301,L]]

Where site.ru is your website domain.

9. Redirect from http:// to https:// and vice versa

A valid SSL certificate must be installed on the site for HTTPS redirection to work.

Redirect requests to https://domain.ru:

RewriteEngine on
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Redirect requests to http://domain.ru:

RewriteEngine on
RewriteCond %{ENV:HTTPS} on
RewriteRule ^.*$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


Troubleshooting

If after editing or adding a .htaccess file your site shows a 500 error, it most likely means there’s a mistake in the .htaccess file. To check: Rename your .htaccess file, for example, to .htaccess_old. Then test your site again. If the site works, the issue is caused by incorrect .htaccess configuration.

You can view the exact cause of the error in the log file: /var/log/your_domain.error_log


Additional documentation and examples

You can find detailed documentation on the official Apache web server website: http://httpd.apache.org/docs/2.4/rewrite/

Всё ещё остались вопросы?