Setting up redirects

Modified on Sat, 11 May at 6:52 PM

GENERAL INFORMATION

Redirection (redirect) is the redirection of a site user to another URL, according to a given rule. It can be set both within one web resource - from page to page, and on another site.

Redirection is most often used:

  • when moving from one domain to another;

  • when transferring part of the content of one site to another;

  • after connecting the SSL certificate (redirecting users from http:// to https://);

  • when transferring data from a subdomain to a domain or vice versa;

  • to redirect visitors from www to non-www or vice versa;

  • to protect against domain spoofing.

A redirect (for example, from domain to domain) can be configured in four ways: through the .htaccess file, PHP, HTML and JavaScript. The rules for .htaccess are universal for any hosting with an Apache web server. If the redirection does not work, then the hosting server is not configured correctly.

Each rule for setting up domain redirection is written to the beginning of the .htaccess file, located in the root directory of the site.

REDIRECTION TO WWW

It is used when the site needs to be translated to an address from www (was http://test.ru, will be http://www.test.ru):

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

REDIRECTION FROM WWW TO WITHOUT WWW

Used to redirect users from the www subdomain to an address without www (was http://www.test.ru, will be http://test.ru):

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

REDIRECTION FROM HTTP TO HTTPS

Used after installing an SSL certificate to redirect all users to a secure https:// connection.

RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

REDIRECTION FROM HTTPS TO HTTP

The rules are used in cases where access to the site must be without a secure connection via http:// (it is worth noting that if there is no SSL certificate installed, it will not work).

RewriteEngine On RewriteCond %{HTTPS} on RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

REDIRECTION TO ANOTHER DOMAIN

A redirect is used when moving from one domain to another (replace http://newtest.ru with the new site address):

RewriteEngine On RewriteRule ^(.*)$ %{REQUEST_SCHEME}://new.ru/$1 [R=301,L]

RENDERING FROM ONE PAGE TO ANOTHER

A redirect can be used both within one site and to redirect a page from one site to another.

Redirect 301 /stranica https://test.ru/stranica/    Redirect 301 /category/stranica https://test.ru/stranica/   

The rule is applicable for any number of pages (written on a new line for each) and is written in the .htaccess of the source site without specifying the domain name.

OPENING A DOMAIN ONLY THROUGH THE MAIN ADDRESS

It happens that many domains are directed to one site (sometimes this is intended, for example, in the case of domain merging, and sometimes attackers do this by directing domains to someone’s server). To ensure that the site opens only at the main address, you can use the following rule:

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

The rule is written into the .htaccess file of the main domain (in our example this is http://test.ru). If the site is requested from any other address, the web server will redirect to the primary address.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article