Javascript redirect to another page. htaccess redirect from subdirectory to another URL


Redirect (from English. redirect– redirect) is a redirection of a website page to some other page or another site. You can do a redirect different ways, the main ones of which will be discussed here, namely redirect to php, javascript, redirect when help html and using the htaccess file.

Among the listed methods, the most relevant are php redirect and redirect using .htaccess. The fact is that these methods allow you not only to redirect the page, but also to return a special 301 error (301 Permament Redirect). Why is this needed? And this is needed for search engines.

Let's say we had a well-promoted website with a large number of visitors. The site was then moved to new domain. The search engine doesn't know it yet. 301 redirect allows you to “glue” the old and new address, while maintaining all the optimization elements that were made for this site, thereby preserving the traffic and audience of the site itself.

Now let’s look at all the listed redirect options, and start with a redirect to php.

PHP redirect (301)

Redirecting to php, as in other programming languages, is based on the specification HTTP protocol, namely sending the necessary headers. How it works? Quite simple. Every time we access a page on the Internet, we receive an HTTP response from the server, which contains headers and a body. In the body of the response

HTML redirect

In language HTML markup There is a special meta tag designed for redirection.

Only the content field changes, where the number of seconds before redirection and the actual link where the redirect will be made are indicated. I don’t think it’s worth saying that the tag is placed in the html code between the head tags.

JavaScript redirect

Perhaps the most unreliable redirect, since the user can always disable javascript in the browser. This is done, however, infrequently, so the method has a right to exist.


function reload() (location = "http://site.com"); setTimeout("reload()", 0);

This code creates a function with redirection to desired page, and then called in the setTimeout function, which allows the redirect to occur after the required time.

htaccess redirect (301)

The .htaccess file is a service file with various additional settings Apache server. It is placed manually, in our case, in the root of the site. With its help, you can arrange 301 redirects, but for this, the necessary modules must be enabled in Apache.

Using mod_alias module directives

There are three directives in this module: Redirect, RedirectPermanent and RedirectMatch. The first two seem to be identical in their properties, the third stands apart. For the first two examples:

Redirect 301 / http://site.com
Redirect permanent /index.html http://site.com
RedirectPermanent /index.html http://site.com/default.html

The lines are similar. But there seems to be one drawback - to redirect all pages, you need to indicate them all in the list. To somehow make the task easier, there is that other directive:

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

You can set regular expressions to redirect from the old URL to the new one.

Using mod_rewrite module directives

To avoid double reading of the same pages by search engines, sometimes you need to forward all addresses from a domain without www to a domain with www. For example,

#enable the module and the necessary additional option
RewriteEngine On
Options +FollowSymLinks
#two lines to redirect from "without BBW" to "with BBW"
RewriteCond %(HTTP_HOST) ^site.com
RewriteRule (.*) http://www.site.com/$1

Instead of a conclusion

If you are still tormented by the question of what to choose (of course, tormented by a vice and scissors - editor's note), then it’s time to stop suffering (what are you saying! – editor's note). Let's focus on the PHP redirect (or other programming language that was not discussed here, for example Perl) and the redirect using the .htaccess file, since they allow you to transfer the site without any loss for optimization in search engines. If it is not important, then any method will do. And actually on this final note (sol? la? si? – editor's note) I am ending this post.

08/28/16 8.5K

In this article, I will tell you how you can redirect a user from one web page to another with using JavaScript. I will also give a few simple examples JS redirect.

You can redirect a user from one web page to any other in several ways. Including by updating HTML metadata, server-side redirection. For example, using a .htaccess file, PHP, and using client side redirection via JavaScript.

But be aware that unexpected redirects that happen in the middle of another activity are annoying for visitors. Therefore, you should use a redirect only if it is really necessary and if it makes sense from the user’s point of view.

Let's look at how JavaScript can be used to redirect the user to another page.

Automatic JavaScript redirect to another page

If you want to automatically redirect the user from one page (URL1) to another page (URL2), you can use the following code:

window.location.href = "URL2";

You need to paste the above code on the first page (URL1). Replace URL2 with required address pages. It's better to place this code inside the element (rather than at the bottom of the page) so that the page redirects before the browser starts rendering it.

TIP: If you are using inline JavaScript (i.e. without an external .js file), be sure to put JavaScript code to tags.

Redirect to another page after X seconds

In this example, we will perform a js redirect to another page some time after the page has loaded. For example, if you need to redirect a visitor to home page after the welcome page is displayed for 5 seconds:

setTimeout(function())( window.location.href = "homepage-url"; ), 5 * 1000);

You need to paste the following JavaScript code on the welcome page. Remember to replace homepage-url with the homepage URL.

We used the setTimeout method to tell the script to redirect after 5 seconds (multiply 5 by 1000 to convert seconds to milliseconds).

TIP: B JavaScript meanings times are always calculated in milliseconds.

Redirect to another page based on condition

For example, you can redirect based on the visitor's browser (although this is not recommended), screen size, time of day, or other condition.

Use the following code to redirect visitors who meet a certain condition:

if (CONDITION) ( window.location.href = "redirect-url"; )

For example, this code redirects visitors to another page if their screen width is less than 600 pixels:

if (screen.width< 600) { window.location.href = "redirect-url"; }

Redirect to another page based on user actions

The last example demonstrates how to retarget a visitor based on their actions. You can bind a js redirect to any type of user action. IN in this example For simplicity, we will handle the button click.

The following code will redirect the visitor to landing page after clicking #mybutton :

document.getElementById("mybutton").onclick = function() ( window.location.href = "redirect-url"; );

You can do the same using the following code:

Go to Homepage

You can also associate a redirect with any event or user action. Just remember to make sure that your redirects won't frustrate users.

A redirect is an automatic redirection of a user from one address to another. That is, a person goes to one site, but ends up on a completely different one (or on another page of one site). I think you've seen this quite often. Sometimes the redirect is done with a delay. In general, the topic is very important, and I will discuss it in this article.

Generally speaking, we will now talk about the Location object, which is a property of the Document object. The Location object has an href property, which is used to implement a redirect to JavaScript. This property available for both reading and writing. First, let's read it:

Document.write(document.location.href);

As a result, you will see the full address to your script.

Now let's make a simple redirect to JavaScript:

Document.location.href = "http://site";

Thus, all users who run this script will automatically go to the site: "http://site".

Now let's do a classic task that is implemented very often. Let's say you had a website: http://a.ru. Then you bought a new domain for your website and its address became: http://b.ru. And you want all visitors to move from http://a.ru to the new http://b.ru. Moreover, you want them to know that your site has a new address. Is the situation familiar? So, this is implemented using a redirect with a delay:


var delay = 5000;
setTimeout("document.location.href="http://b.ru"", delay);

Our website has a new address: http://b.ru. After 5 seconds you will be redirected to it. If this does not happen, then go to: http://b.ru

First, the user will see the message, and after 5 seconds he will go to the new address. If suddenly the user has JavaScript disabled, then he can navigate on his own by simply clicking on the link.

Recently there was an article in which I said that an affiliate program for Pastukhov’s bases had appeared (read -). There I mentioned that Max made it possible to cleverly hide referral links. You can now hide your referral links through the interface affiliate program through php redirect, javascript redirect and .

What is a redirect anyway?

A redirect is when, using special technical tools, we set up a user redirect from one page to another page, or to another site

As you can see, there are a wagon and a small cart of redirect methods. But I will say right away that not all types of redirect are perceived equally loyally by search engines.

For example, a javascript redirect and an html redirect are kind of dumb ways to redirect a page. The fact is that the attitude of search engines to redirects is, to put it mildly, ambiguous. That is, I do not presume to say that a redirect via javascript will definitely lead to a ban, but still such a possibility cannot be ruled out.

In general, it is believed that the safest type of redirect is a 301 redirect. Search engines are more loyal to it. Therefore, of the most common redirection methods, the safest are redirect via htaccess and php redirect. This is because they not only redirect the user to the desired page, but also display special code errors - 301 Permament Redirect. And this is good for search robots.

Yes, by the way, about security, about the fact that 301 redirect seems to be the most reliable, and all others, including redirect through html code are not reliable. There is an example. Website of an authoritative person - Alexey Vostrov (http://www.seoded.ru/). Alexey's website is made in html. I noticed a long time ago that he has everything external links converted to internal ones. Well, for example, how. Where the anchor “Go to another site” is, the external link is disguised as an internal one, and the redirection is implemented through an html redirect

That is, this means one of two things:

1st: either search engines are loyal to html redirects organized only on static sites

2nd: either html redirect is safe for all types of sites, including dynamic ones, using PHP

But to accurately answer this question, you need to do experiments. And to do a normal experiment, you need at least 100 sites, because if a search engine bans one site for using, for example, a redirect via javascript, then this can be considered an accident, and if it bans 98 sites out of 100 for using the same type of redirect, then this will already be a pattern. But since I do not have such resources, I will base my opinion only on assumptions and the majority of opinions that can be found on the forums. And there they believe that 301 redirect is the most reliable

The only thing that can be said about the advisability of using this or that type of redirect is that not all search engines understand redirects via javascript. And this can be used to give more or less weight individual pages site. Remember, I wrote an article about a program for determining weight pages Page Weight? There I spoke in detail about all these things. You can read it here -. As for Google, this search engine understands links via javascript (well, except perhaps for the most cunning ones), but whether Yandex understands such links is still a question. But if you believe the rumors, then it seems that Yasha has not yet received such links.

Let’s assume that we’ve sorted this out, and with this we’ll finish the educational program about the advisability of using one or another type of redirection and move on, in fact, to practice. Let's see how exactly they are configured different types redirect.

Redirect html

1

In general, you could already see the code for this type of redirection above in the screenshot. The only thing you need to pay attention to here is the “ content" Meaning " 0 » can be changed. It means the number of seconds before redirection. For example, if we put content=1, then the redirection will occur in one second. Will redirect to the site specified in the field url. We enter our referral link in this field.

Now step by step how all this is done. Open Notepad, aka notepad. I generally standard notepad, which is already installed by default in the system, I do not use. I use Notepad2. This is the same notepad, only with expanded functionality. But this is true, by the way.

In general, we write the code that I indicated above into this file, call it (the file) “patnerka”, save it with the extension .html and upload it to a folder specially created for this. Give the folder a name. For example, "my_papka". And after that, on all pages of our website where we need to disguise the referral link to the URL “partnerka.com?refid=183”, we simply indicate the address where the html file on our website. That is, in our case it is “_http://mysite.com/my_papka/partnerka.html”. And that’s it, now when you click on a link like _http://mysite.com/my_papka/partnerka.html the user will be redirected to the address _http://partnerka.com?refid=183

PHP redirect (301)

By the way, along the way, I came up with another idea about why you should use a redirect. It may be useful for those who sell links from their sites on the exchange. That is, it is important that the page has as few external links as possible. So we make it so that we have very few external links, or none at all. That is, we actually convert external links into internal ones.

In the case when we did an html redirect, we created separate file, which was uploaded to the hosting. IN in this case all the same. Create a go.php file and add the following code to it:

1 2 3 4 5 6 7 8 9 10 11 < meta http- equiv= "content-type" content= "text/html; charset=UTF-8" />Redirection< ?php $url = isset ($_REQUEST [ "url" ] ) ? $_REQUEST [ "url" ] : "" ; if (preg_match ("#(http?|ftp)://\S+[^\s.,>)\];\"\"!?]#i" , $url ) ) ( sleep (0 ) ; //header("Location: http://partnerka.com?refid=183"); echo "" ; exit () ; ) ?>

Redirection< ?php $url = isset($_REQUEST["url"]) ? $_REQUEST["url"] : ""; if(preg_match("#(http?|ftp)://\S+[^\s.,>)\];\"\"!?]#i",$url))( sleep(0); //header("Location: http://partnerka.com?refid=183"); echo ""; exit(); ) ?>

Here you can also set the forwarding delay using the sleep (0) parameter. Instead of zero we set, for example, the value 1, and the delay during redirection will be 1 second.

Save this file and then upload it to the root folder of the site. If this is a WordPress blog, then upload it to the folder where wp-config.php is located.

I mean, what did I do? In the above code, in the Location parameter, I added the URL with my referral link. The only negative here is that you will have to create a separate file for each referral link.

By the way, I wrote at the top that for referral links we create on the server separate folder. And they named this folder “my_papka”. So, you need to remember to close this folder from indexing using robots.txt.

Well, then, just like in the previous example, in order to direct the user to our disguised referral link _http://partnerka.com?refid=183, we simply indicate instead url address location of our go. php on our server.

JavaScript redirect

This type of redirect is done by analogy with the previous one. Create a new one text file via Notepad2, where we enter the following code:

1 < html>< head>< script type= "text/javascript" >window. location= "http://partnerka.com?refid=183" ;< body

window.location="http://partnerka.com?refid=183";







2024 gtavrl.ru.