How to set up redirects

You can set up redirects using the site's .htaccess file. This article lists commonly used redirects. For their work, you must have:

  • standard VPS + Apache with the mod_rewrite module enabled,
  • a standard VPS with the VestaCP or ISPmanager panel installed in automatic mode.

ATTENTION! The redirects described in this article are not suitable for OS Bitrix7.

In ISPmanager you can also use built-in panel tools for redirects.  

Redirects are set in the .htaccess file in the <IfModule mod_rewrite.c> block:

<IfModule mod_rewrite.c>

Options +FollowSymLinks

RewriteEngine on

#Rules

</IfModule>

The rules include:

RewriteCond - condition,

RewriteRule - rule.

Redirect to another domain

To redirect from www.domain-1.com to www.domain-2.com enter:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www\.)? domain-1\.com$

RewriteRule ^(.*)$ http://www.domain-2.com/$1 [R=301,L]

Redirect from a domain without www to a domain with www

To redirect from domain.test.com to www.domain.test.com enter:

Option 1, for a specific domain

RewriteCond %{HTTP_HOST} ^domain\.test\.com$ [NC]

RewriteRule ^(.*)$ http://www.domain.test.com/$1 [R=301,L]

Option 2, without specifying any specific domain

RewriteCond %{HTTP_HOST} !^www\.(.*) [NC]

RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Redirect from a domain with www to a domain without www

To redirect from www.domain.test.com to domain.test.com enter:

Option 1

RewriteCond %{HTTP_HOST} ^www\.domain\.test\.com$ [NC]

RewriteRule ^(.*)$ http://domain.test.com/$1 [R=301,L]

Option 2

RewriteCond %{HTTP_HOST} !^domain\.test\.com$ [NC]

RewriteRule ^(.*)$ http://domain.test.com/$1 [R=301,L]

Redirect to subdirectory

To redirect from the www.test.domain.com subdomain to the test subdirectory enter:

RewriteBase /

RewriteCond %{HTTP_HOST} ^test\.domain\.com$

RewriteCond %{REQUEST_URI} !/test/

RewriteRule ^(.*)$ /test/$1 [L]

Redirect from http to https

1-1. To redirect from http://domain-1.com to https://domain-1.com enter:

RewriteBase /

RewriteCond %{HTTPS} !on

RewriteCond %{HTTP_HOST} ^domain-1.com$

RewriteRule ^(.*)$ https://domain-1.com/$1 [R=301,L]

1-2. For VPS with VestaCP panel enter:

RewriteCond %{SERVER_PORT} !^443$

RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

1-3. For VPS with ISPmanager panel using Nginx enter:

RewriteBase /

RewriteCond %{HTTPS} off

RewriteCond %{HTTP:X-Forwarded-Proto} !https

RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Redirect from http to https: // www

To redirect from http://test.com to https://www.test.com enter:

RewriteBase /

RewriteCond %{HTTPS} !on

RewriteCond %{HTTP_HOST} ^ test.com$

RewriteRule ^(.*)$ https://www.test.com /$1 [R=301,L]

To redirect from http://test.example.com to https://www.test.example.com enter:

RewriteCond %{HTTP_HOST} ^test\.example\.com$ [NC]

RewriteRule ^(.*)$ https://www.test.example.com/$1 [R=301,L]

RewriteBase /

RewriteCond %{HTTPS} !on

RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

Redirect from http://www to https:

To redirect from http://www.test.example.com to https://test.example.com enter:

RewriteCond %{HTTP_HOST} ^www\.test\.example\.com$ [NC]

RewriteRule ^(.*)$ https://test.example.com/$1 [R=301,L]

RewriteBase /

RewriteCond %{HTTPS} !on

RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

Redirect from https to http

RewriteBase /

RewriteCond %{HTTPS} on [NC]

RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

Redirect to http for one page

Option 1:

RewriteBase /

RewriteCond %{HTTPS} on [NC]

RewriteCond %{REQUEST_URI} ^/required.directory.page$

RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

Option 2:

RewriteBase /

RewriteCond %{HTTPS} !on

RewriteCond %{REQUEST_URI} !^/required.directory.page$

RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS} on [NC]

RewriteCond %{REQUEST_URI} ^/required.directory.page$

RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

For example, to disable the redirect to https for the bitrix/user/1c_exchange.php page for correct operation of data exchange with 1C, enter:

RewriteBase /

RewriteCond %{HTTPS} !on

RewriteCond %{REQUEST_URI} !^/bitrix/user/1c_exchange\.php$

RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS} on [NC]

RewriteCond %{REQUEST_URI} ^/bitrix/user/1c_exchange\.php$

RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

To disable the redirect to https for the same page for the VestaCP panel, enter:

RewriteCond %{SERVER_PORT} !^443$

RewriteCond %{REQUEST_URI} !^/bitrix/user/1c_exchange\.php$

RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

  

RewriteCond %{SERVER_PORT} ^443$

RewriteCond %{REQUEST_URI} ^/bitrix/user/1c_exchange\.php$

RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

Redirect to https for one page

RewriteBase /

RewriteCond %{HTTPS} !on

RewriteCond %{REQUEST_URI} ^/required.directory.page$

RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

If you have any questions, please create a ticket to technical support.