Redirects (or redirects) are used in cases where it is necessary to transfer site users or search engines from one page to another, in other words, to redirect from one URL to another.

Apache

Connecting the mod_rewrite module

To enable redirection using Apache tools, it is enough that the module mod_rewrite.so it was uploaded to Apache.

For Ubuntu, Debian OS:

LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

For CentOS, RedHat, AlmaLinux OS, you need to check that the /etc/httpd/conf.modules.d/00-base.conf file contains the line:

LoadModule rewrite_module modules/mod_rewrite.so

If it is not present, then you need to add it to the end of the file. If it is commented out (there is a # at the beginning of the line), you need to uncomment the line.

In order for the mod_rewrite directives to be used in .htaccess, it is necessary to write in the Apache configuration file in the corresponding section <Directory /path/to/directory>:

AllowOverride all

After making changes to the Apache configuration file, you need to restart the web server in order for these changes to take effect.

For Ubuntu, Debian OS:

systemctl restart apache2

For CentOS, RedHat, AlmaLinux OS:

systemctl restart httpd

In .htaccess, for redirection to work, you need to specify the following directive:

RewriteEngine On

Redirect Rules

You can specify these directives either in the Apache config for the desired virtualhost, or in a file.htaccess.

Redirect or RedirectPermanent

The main disadvantage of these rules is that a new rule must be written for each address. If you need to make multiple redirects, then each new redirect is written from a new line.

Redirect 301 /old-page.html http://new-domain.ru/new-page.html

or

Redirect permanent /old-page.html http://new-domain.ru/new-page.html

To redirect all requests to another site, you can use the following construction:

Redirect 301 / http://new-domain.ru/

or

Redirect permanent / http://new-domain.ru/

RedirectMatch

This redirect differs in that it can use a regular expression. For example, when migrating a website from Windows to Linux, you need to change all links from *.php to *.aspx.:

RedirectMatch /(.*)\.aspx$ /$1.php

RewriteRule

For this module to work, make sure that the FollowSymLinks option is enabled. This function must be registered in the Apache configuration file or in the file.htaccess as specified below.

Let’s look at the most common ways to use it.

Redirect from one site to another

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.oldsite\.ru [OR]
RewriteCond %{HTTP_HOST} ^oldsite\.ru
RewriteRule (.*) https://newsite.ru [R=301,L]

Redirect from www to without www

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

Or a more understandable syntax:

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

You can use any one.

Redirect from without www to www

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

The alternative:

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

Redirecting a domain from https to http

In order for this redirection to work, only the Apache Web server must be used. When using the Nginx+Apache bundle, a round-trip error will occur. Therefore, the redirect will need to be configured specifically in Nginx.

RewriteEngine On
RewriteCond %{SERVER_PORT} ^443$ [OR]
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

Redirecting a domain from http to https

In order for this redirection to work, only the Apache Web server must be used. When using the Nginx+Apache bundle, a round-trip error will occur. Therefore, the redirect will need to be configured specifically in Nginx.

RewriteEngine On
RewriteCond %{SERVER_PORT} ^80$ [OR]
RewriteCond %{HTTP} =on
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]

Nginx

The ngx_http_rewrite_module module, required for configuring redirects, is installed automatically along with Nginx.

It is recommended to add redirect rules one at a time, because you may get an error or a cyclic redirect. Therefore, after each change to the configuration file, you need to check its correctness with the command:

nginx -t

Redirect 301 s www.domain.com on domain.com

For Nginx, you need to create two server sections in the configuration file, one for the domain with www, the second for the domain without www:

The server section for redirects:

server {
     listen  80;
     server_name www.domain.com;
     return 301 $scheme://domain.com$request_uri;
}

The server section, where the basic domain settings are located:

server {
     listen  80;
     server_name domain.com;
.....
}

After making changes to the Nginx configuration file, you need to restart the web server in order for these changes to take effect.

nginx -s reload

or

systemctl reload nginx

Redirect 301 s domain.com on www.domain.com

For Nginx, you need to create two server sections in the configuration file, one for the domain with www, the second for the domain without www:

The server section for redirects:

server {
     listen  80;
     server_name domain.ru;
     return 301 http://www.$host$request_uri;
}

The server section, where the basic domain settings are located.

server {
     listen  80;
     server_name www.domain.com;
.....
}

After making changes to the Nginx configuration file, you need to restart the web server in order for these changes to take effect.

nginx -s reload

or

systemctl reload nginx

301 redirect from https to http

For Nginx, you need to create two server sections in the configuration file, one for https (port 443), the second for http (port 80).

The server section for opening over https (port 443) and redirect settings:

server {
    listen  443;
    server_name domain.ru www.domain.ru;
    return 301 http://$host$request_uri;
}

The server section for opening over http (port 80), where the main domain settings are located.

server {
   listen  80;
   server_name www.domain.com;
.....
}

After making changes to the Nginx configuration file, you need to restart the web server in order for these changes to take effect.

nginx -s reload

or

systemctl reload nginx

301 redirect from http to https

For Nginx, you need to create two server sections in the configuration file, one for http (port 80) and the other for https (port 443).

For the new domain in the nginx conf file

The server section for opening over http (port 80) and configuring redirects:

server {
    listen  80;
    server_name domain.com www.domain.com;
    return 301 https://$host$request_uri;
}

The server section for opening over https (port 443), where the main domain settings are located.

server {
    listen  IP.server:443;
    server_name www.domain.com;
.....
}

For an existing domain in the nginx conf file

If you are making changes to an existing section of the nginx conf file, do it like this:

delete the following line from the main domain section

 listen  IP.server:80;

and create a new server section of this type:

server {
    listen  80;
    server_name domain.com www.domain.com;
    return 301 https://$host$request_uri;
}

After making changes to the Nginx configuration file, for these changes to take effect, you need to restart it like this:

nginx -s reload

or

systemctl reload nginx