Redirect .htaccess: redirect to another domain and redirect to another page

  1. Redirect .htaccess: permanent redirection, 301
  2. How to set up a redirect to www (or without www)
  3. How to create .htaccess
  4. The .htaccess file does not work
  5. Is there any analogue of .htaccess in nginx?
  6. Publisher
  7. x64 (aka andi)

Good Sunday!

Very often in the forums you can find questions on .htaccess - the Apache web server configuration file. The main stumbling block is that the development did not have the task of convenient configuration for the end user (the distribution does not include a shell for visual installations using checkboxes, drop-down lists, etc.), but server administrators cope with this task remarkably, being content with "mana" (from the Linux team - man - a quick tutorial from developers, or in this case from professionals to professionals).

In most cases, simple manipulations are required: unconditional redirect to another site, or redirection from one place to another (say, when the URL of the section has changed). And each person starts to believe that there’s definitely no problem anywhere, and it’s even useless to search for a solution on the Internet and search the forum. Let's see what these problems really are not.

Let's see what these problems really are not

Redirect .htaccess: permanent redirection, 301

This thing has different names. Neponyatki can cause perhaps that number - why is 301? The essence lies in the HTTP protocol itself, which responds to client requests with a specific status code. The 404 Not Found code is known to almost everyone. The 200 OK code is almost unknown, but it means that everything is in order and the document will be shown in the browser. But the code 301 Moved Permanently means that the document finally moved to a new address. This is what is often called redirection, although the total response pool is designated as - 3xx: Redirection .

Apache has several options for redirects. The mod_rewrite module provides an excellent mechanism for working with redirects, and in the future I will use it in the examples. The mod_alias module also has directives for redirects from a specific address or from an address that matches the regular expression:
Redirect permanent / http://newsite.ru/
RedirectMatch permanent. * Http://newsite.ru/
But mod_rewrite has additional options for redirection, so it’s best to use (IMHO) it.

In general, for an unconditional external (let's call it user) redirect, in which the browser automatically redirects to another page, usually imperceptibly for the user (the address bar changes), you can put such code in .htaccess:
RewriteEngine On
RewriteRule. * Http://newsite.ru/ [L, R = permanent]
Here, the first line includes the mechanism of the mod_rewrite module, which is allowed to do anything with the addresses. Let's call this URL conversion by conditions on the fly. The second line directly defines the rule for conversion; in the example above, all requests will be redirected to http://newsite.ru/. Flags are placed in square brackets: L - last (Last) rule, R - redirect type (Redirect), indicated as R = code, where code is an alphabetic or numeric designation (permanent or 302).

In fact, mod_rewrite is a very useful thing, because it is because of this that many engines actively use the so-called CNC (Human URL Understandable). If you are unfamiliar with this term, I will explain with an example. Article about free engines for the site has the address: https://a-panov.ru/2014/11/free-cms/

Looking at this URL, it is immediately clear that the article was published in November 2014. By removing the title with a trailing slash - https://a-panov.ru/2014/11/ - we will receive a list of all the publication for November. Normal addresses, type:

https://a-panov.ru/?p=389

no special friendliness. Seeing such an address, you can not say about the page on which he leads, absolutely nothing.

This method is good when you don't care which page of the new site to link to. But if you moved to another domain, it is advisable to redirect all requests while maintaining addresses. To do this, use the following code:
RewriteRule ^. * $ Http://newsite.ru/$0 [QSA, L, R = permanent]
The new QSA flag will also save the parameters that can be found in the addresses after the question mark. In the already familiar example:

https://a-panov.ru/?p=389

if I place the code in .htaccess above, then there will be a redirection to the address http://newsite.ru/?p=389 which without this flag would not exist.

Hereinafter, I will omit the transformation inclusion directive:
RewriteEngine On
just remember: if you use the mod_rewrite mechanism, then all conditions / conversions must be performed after this directive. To disable the following line is used:
RewriteEngine Off
accordingly, for each rule there is no need to constantly “turn on” transformations - it starts to be done automatically, immediately after the first directive.

How to set up a redirect to www (or without www)

Such redirections are required for cases when the site is available on the options nsite.ru and www.nsite.ru, in this case, the search engines consider them duplicates and can either choose one of the options as main mirror , or in the search there will be options from both domains. In general, this is a negative factor when ranking a site.

Option easier for one domain. Redirection is performed from the subdomain www.site.ru to site.ru:
RewriteCond% {HTTP_HOST} ^ www \ .nsite \ .ru $
RewriteRule. * Http://nsite.ru/$0 [QSA, L, R = permanent]
This is where the new RewriteCond directive appeared, which sets the condition under which redirects should work - the RewriteRule rules. The condition, in this case, the domain name corresponding to www.nsite.ru (in regular expressions, the dot symbol means “any character”, so it is advisable to shield it with a backslash).

Reverse redirection is also very simple:
RewriteCond% {HTTP_HOST} ^ nsite \ .ru $
RewriteRule. * Http://www.nsite.ru/$0 [QSA, L, R = permanent]
Both options are working, but they have a small minus - for each new config it is necessary to replace the nsite.ru domain with your own. Is it possible to make a unique option? Easy!

Redirect from any www subdomain:
RewriteCond% {HTTP_HOST} ^ www \. (. *) $
RewriteRule. * Http: //% 1 / $ 0 [QSA, L, R = permanent]

Redirect to a subdomain with www:
RewriteCond% {HTTP_HOST}! ^ Www \.
RewriteRule. * Http: //www.% {HTTP_HOST} / $ 0 [QSA, L, R = permanent]

% {HTTP_HOST} - current domain

How to create .htaccess

Such questions also arise. The problem is that the standard Explorer (the application in Windows, which is responsible for the graphical interface) does not allow you to create files starting with a dot (creating files or folders by right-clicking on the desktop or in a window and selecting the desired action from the Create list). But there is a way out: open the standard Notepad recorder, select File → Save as ... in the menu, and specify .htaccess as the name of the saved file - after that it will be created.

The .htaccess file does not work

There are cases when directives from a file do not work. This is usually caused by the fact that the .htaccess support is disabled in the Apache configuration, for which the directive is responsible:
AllowOverride none

This is usually done to increase the speed somewhat - in this case, the web server does not search and parse .htaccess.

It is also possible that Apache is not used on the server, for example, IIS is the main “guest” on Windows hosting. For clarification, contact the support of your hoster.

Is there any analogue of .htaccess in nginx?

Another fairly frequent question. In this web server, this is not possible. However, this is usually not required. The fact is that nginx is usually installed as a frondend, that is, it accepts and processes all requests that it either performs itself or redirects to the backend, which can be performed by Apache, which can be further configured using .htaccess.

Publisher

not online 1 day

x64 (aka andi)

Comments: 2834 Publications: 394 Registration: 02-04-2009

Htaccess in nginx?
Neponyatki can cause perhaps that number - why is 301?
Ru/?
Ru/?
Ru/?
Is it possible to make a unique option?
Htaccess in nginx?