How to make a page with a redirect. Redirecting URLs with JavaScript

Hello everyone!

The theme of the past is . I briefly talked about his most important points and mentioned several functions that this very file performs. One of these functions is the automatic redirection of a visitor from one URL to another, also called a 301 redirect.

What is a 301 redirect?

As I already said, a 301 redirect is an automatic redirect that allows you to redirect a visitor or search robot from one page of a site to another or from one domain to another. With all this, all the so-called weights (page weight) are transferred to the URL to which the redirect occurs.

Thus, we get a real lifesaver when moving a site to another domain and other situations, which we will discuss below. In this case, we will lose practically nothing.

Example. Previously, the Context-UP blog was located at the URL: http://blog.site (among you, there are probably those who saw these times), then I wanted the site to be on a second-level domain, that is. To fulfill my desire, I had to transfer all the contents of the blog to another directory (folder) on the hosting, then in the panel, and, indicate new domain so that the robots start indexing new address blog. Only after all this, I set up a redirect to..

Now I hope you understand what a 301 redirect is.

How to set up 301 redirect via htaccess?

There are several ways to implement a redirect, but the safest and easiest is a 301 redirect in .htaccess.

So, in order for the file and redirection, respectively, to work, it is necessary to activate the so-called modules. For our purpose, we can use two:

  • Mod_rewrite;
  • Mod_alias.
  • The first option is the most preferable, since when using it you will not need to register each URL page separately, but just use special teams(they are below), thanks to which the redirect will occur.

    We, that is simple clients hosting providers are only able to activate this module for their website directory through the .htaccess file. This is done simply, just enter the line in the previously mentioned file:

    RewriteEngine On

    This module will only work if it is connected to Apache web server. Therefore, please check at the moment from administrators.

    As part of this lesson, I want to look at several situations in which we will need to set up a 301 redirect:

  • If you are moving from one domain to another;
  • If your site is accessible with www and without www;
  • From http to https protocol;
  • From one page to another;
  • WITH index file index.html/php to the main page.
  • Let's go in order.

    Moving to another domain

    If you decide to move to another domain, then you need to write the following command in the additional configuration .htaccess file:

    RewriteEngine On RewriteCond %(HTTP_HOST) old-domen.ru RewriteRule (.*) http: //new-domen.ru/$1

    301 redirect from www to non-www URL and vice versa

    A search robot is a complex thing, so when working with a website, a webmaster needs to take into account many nuances. As an example, for search engine the site, which is accessible at the address with www (www.site.ru) and without tridouble (site.ru), is divided into two completely identical, duplicating each other, web resources.

    For this reason, any website should be accessible only at one address: either with www or without. Otherwise, the site’s position will not grow.

    Redirect from www.site.ru to site.ru:

    Redirect from site.ru to www.site.ru:

    RewriteEngine On RewriteCond %(HTTP_HOST) ^www\.(.*) RewriteRule ^(.*)$ http://%1/$1

    From http to https protocol

    Use this code to redirect to secure http protocol s:

    RewriteCond %(HTTPS) off RewriteRule ^(.*)$ https: //%(HTTP_HOST)%(REQUEST_URI)

    From one website page to another

    Very often it happens that you need to redirect site visitors and search robots from one page to another. In this case, it is enough to write the following code in htaccess:

    Redirect 301 /page1..html

    At one time, I needed to make a 301 redirect from a page generated by the WordPress CMS to the main page, since the first one duplicated what was on the second one.

    From index.html or index.php to the root (slash)

    Initially, on almost all sites, the main page is available at two URLs: http://site.ru/index.php and http://site.ru/. This is not correct, as it results in duplicate content home page and the PS will begin to pessimize the site in the search results (deliberately lower the site down).

    To prevent this from happening, you need to write the following:

    If you want to redirect from index.html

    RewriteCond %(THE_REQUEST) ^(3,9)\ /index\.html\ HTTP/ RewriteRule ^index\.html$ http: //site/

    If you want to redirect with index.php

    RewriteCond %(THE_REQUEST) ^(3,9)\ /index\.php\ HTTP/ RewriteRule ^index\.php$ http: //site/

    Use this material, as a cheat sheet, in case you need it. Just copy it required code and paste it into .htaccess.

    Well, that's all, dear friends!

    Don't forget to subscribe to blog updates and share links to lessons on social networks.

    See you soon!

    Previous article
    Next article

    Instructions

    It is possible to send to another site only those visitors who request certain type. To do this, you need to use another directive - RedirectMatch. It differs from the Redirect directive in that it uses the request and the conditions written in htaccess to compare regular expression(regexp). For example: RedirectMatch (.*).html$

    To put this redirection method into practice, open a simple text editor(Notepad) and create in it blank document. Create the required condition based on the given rules and write it down in this document. Then save it as ".htaccess" and upload it to root directory your website. This will complete the procedure.

    Sources:

    • Redirect 301: How to redirect from one page to another
    • 301 redirect from page to page without affecting others

    Organize a permanent automatic transition all visitors to one specific page to another site you can either use the server and server programming languages, or with using HTML and JavaScript. The advantages of the second option are simplicity and accessibility - its implementation does not require programming knowledge; the only mandatory requirements are access to editing source code pages.

    Instructions

    You can solve the problem of automatically redirecting visitors to another site only using HTML (HyperText Markup language- “hypertext markup language”). It has a (meta tag) that tells the browser that after loading current page you should start downloading another one. This meta tag contains information (tag attributes) about the redirection address and the time after which the website should be sent to the page. For example, it may look like this: Here Refresh is the code word that triggers the redirection mechanism. The number 5 indicates that the process should begin after loading this page. This time may be necessary so that the visitor, for example, has time to send a message that you will place on this page. If such a pause is not needed, set it to zero. And URL=http://www.site contains the address to which the browser should send the visitor. This meta tag should be placed in the header part of the page source code - between and .

    Another way is implemented using the language JavaScript programming. You only need one line of code to get your web surfer to required address. It may look, for example, like this: window.location.reload("http://www..location.replace("http://www..location.href="/";Here you only need to replace the address of the topics which you should place this command inside tags that tell the browser that it is written in JavaScript:
    document.location.replace("http://www.site");
    And these three lines, in turn, should be placed inside the same header area (between and ).

    Once you select one of these options, open desired page, for example, in the page editor of the site management system. Switch to HTML editing mode and find the tag in it. Copy the prepared redirect code (JavaScript or HTML) and paste it before this tag. Then save the modified page.

    Quite complex and interesting topic. Many, almost all, novice bloggers have no idea what a 301 redirect is. I have already touched on this topic in an article about duplicate content and now we will look at it in more detail.

    What is a 301 redirect? This special code, which is returned by the server when a specific URL is accessed.

    What does it mean? This means that if you typed in address bar browser specific URL, you will be redirected to the new address.

    What is all this for? Let's take an example from real life. You go to a certain address, and they say to you: “But people like that don’t live here anymore!” They moved their entire property to another street and here is their new address...” Agree useful information? It was not for nothing that I said “...with all their property,” that is, they didn’t leave anything at the old address. This is important!

    If for some reason your project has moved to a new address, then a 301 redirect will tell you search robot where to look for you, and your site will not lose weight and other indicators when moving.

    We register a 301 redirect in the .htaccess file

    To perform this action, you need to write the following in the .htaccess file:

  • Redirect 301 / http://new site.ru
  • Where “/” means that everything starting from the “head” of the site including all subdirectories will be redirected to the new address.

    Don't forget to do backup copy site if suddenly something goes wrong!

    Now let's consider another situation. I have 2 apartments, but I only live in one. Letters come to me at a different address, my phone rings, people come in different people etc. And I forgot to put up notices that I was in a completely different place. Funny story! It turns out that I am losing my own authority in the eyes of others because of my forgetfulness.

    This is the conclusion. It is imperative to indicate to the search robot the exact address of the project, for example: www.site.com or just site.com because these are two different addresses.

    How to do this, see this example:

    Redirect from www to non-www

  • Options +FollowSymLinks
  • RewriteEngine On
  • RewriteCond %(HTTP_HOST) ^www.site\.com$
  • RewriteRule ^(.*)$ http://site.com/$1
  • Redirect from without www to with www

  • Options +FollowSymLinks
  • RewriteEngine On
  • RewriteCond %(HTTP_HOST) ^site\.com$
  • RewriteRule ^(.*)$ http://www.site.com/$1
  • It should be noted that if the line .htaccess is already written RewriteEngine On, then you don’t need to repeat it and don’t forget to enter your domain.

    There is an opinion that from a protected https protocol:// also needs to redirect users to the main protocol http:// I even contacted the support service of my hosting provider with this question. To which I was told that redirection is possible and for this you need to write the following in the .htaccess file:

  • RewriteEngine On
  • RewriteCond %(SERVER_PORT) ^443$
  • RewriteCond %(HTTPS) =on
  • RewriteRule ^(.*)$ http://yoursite.com/$1
  • There are times when it is necessary to transfer only one page while maintaining its weight. To do this, write in the .htaccess file:

  • Redirect 301 /path/post.html http://newsite.com/post.html
  • In this article, I tried to describe the most common cases of using 301 redirects that every webmaster needs to know. In fact, the possibilities of 301 redirects are not limited to this, and in the future I will return to this topic more than once, so I advise you to subscribe to blog updates.

    See you!

    Best regards, Vitaly Kirillov

    In the material below I will describe six ways to redirect a page.

    I have no doubt that many have already heard about such a concept as a redirect, and roughly know what it is needed for. But not everyone knows how to do it correctly.

    This material does not represent an exhaustive set of redirects - these are just a few of the most popular types and redirect methods.

    Why do you need a redirect? The main function is to redirect visitors from one site to another (from one page to another).

    The reason for this may be different circumstances, for example, changing a promoted domain, traffic arbitrage, working with affiliate programs.

    The essence of how a redirect works is as follows: the user types one site address, presses ENTER, and ends up on a completely different site.

    Let's look at the following types of redirect:

  • header redirect using PHP— 301, 302nd redirect (the difference between them).
  • Redirect using .htaccess (and the mod_rewrite module, which is used for this).
  • The simplest JavaScript - redirect using document.location
  • META-REFRESH - redirect using meta tags
  • JavaScript redirect with delay via setTimeout (for example, for accounting by Metrica).
  • Iframe/Frameset - “pseudo” redirect through frames (for example, to pass moderation in Yandex.Direct).
  • Redirect - redirection

    How to redirect a page. Method 1 - PHP redirect.

    This redirect works without loading the page.
    The page to which the redirect is sent receives the original REFERER (that is, in this way it is not possible to hide where the redirect came from).

    For example, this redirect can be used for YouTube. YouTube, as you know, can only be linked to one site, so you won’t be able to link to someone else’s site. To bypass this limitation, create a separate subdirectory on your domain and put index.phd in it with the specified redirect code.

    The redirect must be executed in the .php file and MUST before any characters are output. That is, if there is extra space or a character that is displayed on the screen (for example, before the sign