Redirects to website

These redirects assume the Apache server and work in the .htaccess file. It is placed at the root of the site and can be overloaded in subdirectories. PHP commands can also be used.

Resume Summary

Redirect against URL rewrite

It is necessary to distinguish the principle of redirection from the principle of rewriting URLs. These are two methods that are close but have the opposite effect:

Redirection - The URL specified by the user is redirected to a new page. The URL of the new page is displayed by the browser.
We use it to change the address. The new URL must be indexed by search engines, and the old one must be deleted.

Overwrite - The user-specified URL is silently replaced by the server in a new path. However, the browser displays the URL specified by the user.
It is used to separate the visible URL from the real path and provide users with more convenient designations. Search engines should index only the visible URL, not the path on the server.

Apache uses the same RewriteEngine in both cases, but the Redirect command or parameter 301 changes the displayed URL. In fact, the server returns a new URL to the browser that should execute the new request.

Redirects must be placed in an .htaccess file before being overwritten.

How to add www

How to redirect http://example.fr to http://www.example.fr...

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

HTTP_HOST represents a domain, so the condition retrieves the domain from the URL and compares it to a string starting with example.fr.
And we redirect 301 to http://www.example.fr for all addresses starting with example.fr.

How to return 404 errors to the page

This may be an error page, so-called code name page 404, returned when the page is not found, or a home page (more useful).

RewriteEngine  on
ErrorDocument 404 /index.php

Note that the RewriteEngine directive that activates redirects is placed at the beginning of the .htaccess file.

Redirect one domain name to another

To permanently change the domain name, the new domain example.com...

RewriteEngine  on
Redirect 301 / http://www.example.com/  

All pages will be redirected to new files with the same name, but to a new domain.

Code 302 would be used for the temporary change.

WITH PHP :

To redirect the site to a new domain name, you can use the following code in the index.php file:

<?php 
   header("Status: 301 Moved Permanently"); 
   header("Location:http://www.example.com/"); 
?>

The visitor will look like they entered the destination site URL.

For temporary redirection to PHP, this is easier, since this is the default option, and there is no need to clarify the status:

<?php header("Location:http://www.example.com/"); ?> 

Redirect home page to site name

We want a URL like http://www.example.com/index.html to be redirected by the server to the http://www.example.com/ so that search engine robots do not see two different URLs where there is one.

In this case, the PHP environment REQUEST_URI variable is used to indicate which page was requested by the server. The boot command is then used, as for example below:

<?php
 if(eregi('index.(html|php)', $_SERVER['REQUEST_URI'])) 
 { 
  header('HTTP/1.1 301 Moved Permanently'); 
  header('Location: http://www.example.com/'); 
 } 
?>

The eregi function verifies that the loaded page is indeed index.php or index.html (or any other name) and only in this case is redirected.

Another solution is to rename the home page, for example, assueil.html, specify it to the server with the following command in .htaccess:

DirectoryIndex accueil.html

Redirect for moved page

Because the page has been moved to a new directory or under a new name (or both), this directive must be passed to the server:

Redirect 301 /répertoire/page.html http://www.example.com/répertoire/page.html  

Whether on the same site or a different site, the site name is always included in the destination URL, not the source .

Redirect the entire directory to a single file

If you want to remove a subdirectory from the Web, you can redirect it to a block on the home page or another page with one directive:

RedirectMatch 301 ^/monsousrep/.*$ http://www.example.com/index.php

The directory to be deleted is called "monsousrep," which should be replaced by the name of the directory, as well as the destination domain and page.
The destination should not be contained in the redirected directory because it would lead to an infinite loop.

From the point of view of references, such an operation is criminally punishable. It is better to put the page in "noindex" rather than redirecting it to another page when the content is different.

When selecting a redirect, as always, make sure that all links pointing to the redirected page are updated. A redirect link on the same site fines.
Use Link Checker with -r-s-f options to find these links.

For more information on redirects in the .htaccess file, see the Apache guide.