When you create a website and/or create a new version of it (with new features, for example), you may need to create redirects to redirect visitors to the new address of a page, ...
However, there are different techniques for redirecting a visitor and some techniques are more suitable for some cases than others.
For example, when updating a site, if a page changes its address (url), you will have to redirect visitors using server redirects (with an .htaccess file or by using PHP) to allow search engines to update their search results without the SEO of your website and more precisely your pages being affected.
If you hosted your website on Linux web hosting (which is the case in most cases), the web server used by default will be Apache.
To perform server redirects with an Apache web server, you will need to create a ".htaccess" file and use the "Redirect" instruction of the "mod_alias" module.
Among the various possible redirects you :
Redirect /folder https://www.my_website.com/folder Redirect permanent /folder https://www.my_website.com/folder Redirect temp /folder https://www.my_website.com/folder Redirect seeother /folder https://www.my_website.com/folder Redirect gone /folder https://www.my_website.com/folder
You can also use the HTTP code corresponding to the redirection you want to perform.
Redirect 301 /folder https://www.new_domain.com/folder Redirect 302 /folder https://www.new_domain.com/folder Redirect 303 /folder https://www.new_domain.com/folder Redirect 304 /folder https://www.new_domain.com/folder
Note : for more informations about HTTP codes, go to the "List of HTTP status codes" page of Wikipedia.
If there are several types of redirects, it is simply because they do not all have the same goal.
For example, if you change your web host and address, we recommend that you create a 301 redirect. This will prevent you from losing your ranking in the search engines (including Google) as this redirection redirect visitors and search engines as well. When Google arrive on the old domain, it will replace the old with the new url without making you lose your position in the ranking.
In this case, you create a file in the root of the website on the old host and you indicate this inside ".htaccess".
Redirect 301 / https://www.new_domain.com/
For more information about server redirects with Apache, we suggest you go read the official Apache documentation at this link.
If you used PHP to create your dynamic website, you can create permanent, temporary, 301, 302, ... redirects with its "header" function.
Note that if you use a PHP framework, such as CakePHP, Symfony, ... you will probably find redirection functions in their official documentation.
Warning : this function must be used before any HTML character.
Indeed, if a space was sent before using this function, PHP will display the error : Headers already sent by ...
To refresh the page and redirect the visitor, you must use the Refresh keyword and specify a time in seconds.
<?php // Redirection informations $delay=3; // Time in seconds $url='/test/'; // Address to redirect the visitor // Redirection in x seconds header('Refresh: '.$delay.';url='.$url); ?>
Warning : don't use this type of redirect if you also want to redirect search engines like Google or Bing, for example.
To create a true redirection, ie by returning a HTTP redirect code, you must first change the "Status" info in the header and then perform the redirect 301, 302 or by another keyword : Location.
Refer to "HTTP/1.1 Specification" for more information on HTTP headers.
<?php // We modify the "Status" information header('Status: 301 Moved Permanently', false, 301); // We perform the redirection header('Location: /test/'); ?>
If you want to redirect your visitors to another page automatically, you can use meta "Refresh" tag that will allow you to set a delay before forwarding if you wish. However, note that this is a redirect which occurs client side (because it is an HTML tag) and not a server redirection (PHP for example).
This meta tag is inserted between <head> and </head> tags of your page.
<META http-EQUIV="Refresh" CONTENT="Time; url=https://www.informatiweb.net/">
To configure this redirection, replace Time by the number of seconds to wait before redirecting the visitor and the new address after "url=".
This script should be placed between the <body> and </body> tag of your webpage.
In this case, the countdown starts when the page (on which you insert this code) loading.
If you want to redirect the visitor after x seconds from loading of the page. Simply enter this code into your page.
Note : preferably at the end of the page. Before the </body>.
If you want to trigger this redirection after clicking a button or other, we recommend that you place it first in a function. Then simply, to call this function by the desired event.
Here's the function :
And here's how to use it with the click event (or onclick). You can of course use other events like "onmousedown", "onmouseover", ...
To use this function, simply pass the following parameters :
- url : the address to redirect the visitor
- delai : delay in seconds before redirecting
<input type="submit" name="my_button" id="my_button" value="Button text" onclick="redirection_js('/folder/destination_page.php','3')" />
® InformatiWeb.net 2008-2022 - © Lionel Eppe - All rights reserved.
Total or partial reproduction of this site is prohibited and constitutes an infringement punishable by articles L.335-2 and following of the intellectual property Code.