301 redirect (301 Permanent Redirect) – server-side redirection when changing the address of a site or page. This server response allows for automatic redirection of the user to a new address. Search engines understand that the URL has changed with a 301 redirect, and they will soon merge the old and new addresses.
Setting Up a 301 Redirect
1. Through PHP Condition
In this case, the server determines the redirection, and the browser receives the command to open a new address instead of the old one. You can specify the PHP redirect in the root index.php file.
Option 1:<?php header(“HTTP/1.1 301 Moved Permanently”); header(“Location: http://new-domain.com/index-new.php”); exit(); ?>Option 2:
<?php
if($_SERVER['REQUEST_URI'] == "/index-old.php") {
header("Location: /index-new.php",TRUE,301);
exit();
}
?>
2. Through the .htaccess Configuration File
.htaccess is a configuration file for the Apache web server that allows you to manage the server’s operation and settings for individual sites without changing the main configuration file of the web server.
1. Redirecting an old page to a new one:
Redirect 301 /old.php http://www.mediasova.com/new.php
2. Redirecting an old site to a new one
Option 1:Redirect 301 / http://new.mediasova.com/Option 2:
RewriteCond %{HTTP_HOST} ^old.mediasova.com [NC]
RewriteRule ^(.*) http://new.mediasova.com/$1 [L,R=301]
3. Redirecting from www to non-www and vice versa
Redirecting from www.mediasova.com to mediasova.comRewriteCond %{HTTP_HOST} ^www.mediasova.com$ [NC]
RewriteRule ^(.*)$ http://mediasova.com/$1 [R=301,L]
Redirecting from mediasova.com to www.mediasova.com
RewriteCond %{HTTP_HOST} ^mediasova.com [NC]
RewriteRule ^(.*) http://www.mediasova.com/$1 [L,R=301]
4. Redirecting from http to https
Option 1:RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Option 2:
RewriteCond %{HTTP:X-HTTPS} !1
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
5. Redirecting from https to http
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
6. Redirecting all non-existing subdomains to your site
RewriteCond %{HTTP_HOST} ^(.*).mediasova.com [NC]
RewriteRule ^(.*)$ http://mediasova.com/$1 [L,R=permanent]
Additional Information about the .htaccess File:
- Some settings, such as redirecting from http to https or from www to non-www, can be done through the hosting panel without editing the .htaccess file.
- The above rules for 301 redirects are best placed after the two lines if they exist in your .htaccess file:
Options +FollowSymLinks RewriteEngine On
- A comment in the file is denoted by the hash symbol “#” at the beginning of the line:
# Text comment; this line will not be processed
- Redirect methods using .htaccess only work on servers with the Linux operating system and an Apache server with the Mod-Rewrite module enabled.
- Using .htaccess creates additional load on the Apache server. It is more efficient to write the same commands in the httpd.conf configuration file, but usually, webmasters do not have access to it.
SEO Specialist Tip: A 301 redirect is a good method for preserving positions and results in search engines when moving a site to a new address. Proper configuration of the 301 redirect in the .htaccess file also helps avoid the appearance of duplicate pages.
