2.3.3 Redirects. How to set a redirect for your domain

Redirect Banner

Redirects are an important aspect of managing a website. They allow redirecting visitors from one page to another, ensuring convenient navigation and preserving SEO rankings. In this article, we will explore various methods of creating redirects, starting from basic rules in the .htaccess file to configuring them through web servers, helping you effectively manage the flow of traffic on your resources.

Important: redirect configuration syntax is sensitive to the website protocol in use. Below, we provide examples for http://, if your site uses https://, this should be taken into account when configuring redirects.

Redirect via ISPManager Panel

If you are using the ISPManager panel, you can configure redirects in the corresponding Redirects tab.

Go to this section, click Create, and fill in all the fields:

Сделать перенаправление (редирект) домена. ISP

  • WWW domain - select which WWW domain you want to configure the redirect for.
  • Path - specify the relative path (link) you want to redirect to another URL.
  • Code - specify the type of redirection in the form of a code (301, 302, etc.).
  • URL - Specify the absolute URL to which you want to redirect. Provide the full path, including http://.

Redirect via .htaccess

Redirects are often configured through .htaccess files. The rules in each .htaccess file apply imperatively to all files and directories within its location, provided there is no separate .htaccess file in that subdirectory. Configuring redirects using this method is very convenient if different directories on your site require specific redirection rules for their operation.

Here’s an example of a simple redirect using .htaccess:

.htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^old_domain\.com [NC]
RewriteRule ^(.*)$ http://new_domain.com/$1 [L,R=301]

We have a whole article dedicated to examples of 301 redirects via .htaccess. You can read it here.

Note: Redirects via .htaccess require the mod_rewrite module to be enabled. On our hosting and in our OS images with ISPManager panels, it is enabled by default.

Redirect via Web Server Configuration

Redirects can be configured using web server configuration, both Apache and Nginx support this method. We recommend this approach for advanced users, as careless editing of the web server configuration can lead to unpleasant consequences.

Note: syntax and file locations for configuration may vary depending on your operating system and server settings. If you are unsure about the changes you’re making, it’s better to use a different method for setting up redirects.

Example: redirect for Apache

1. Open the configuration file. Usually, it’s either httpd.conf or apache2.conf in the Apache configuration directory.

2. Add the redirect rule. In the Apache configuration file, add the following rule inside the <VirtualHost> block:

Redirect permanent / http://new domain.com/

3. After making changes to the Apache configuration, restart the server to apply the changes:

sudo service apache2 restart
Example: redirect for Nginx

1. Open the Nginx configuration file. Usually, it’s either /etc/nginx/nginx.conf or a file in the /etc/nginx/sites-available/ directory.

2. Add the redirect rule. Inside the server block, add the following rule:

server {
    listen 80;
    server_name old_domain.com www.old_domain.com;
    return 301 http://new_domain.com$request_uri;
}

Here, old_domain.com and new_domain.com are your old and new domains respectively.

3. After making changes to the Nginx configuration, restart the server to apply the changes:

sudo service nginx restart