Set up 301 Redirects in Apache for SEO to redirect your web pages

4 minutes read

If you have changed the domain of your website from www.xyz.com/seo to www.abc.org and you need that your visitors accessing anything in your website www.xyz.com/seo should be redirected to a new domain i.e.  www.abc.org, you can do 301 Redirect in Apache to redirect your web pages to new domain

 

This redirection accommodates several files and folders name changes and is done with 301 redirections which is considered as Search Engine Friendly. 301 Redirects in Apache is also done to overcome the canonicalization issues. The simple way to add 301 redirects in PHP file of www.abc.org is to include apache module mod_rewrite that matches specific patterns for entire folders and redirect them to their new URLs without having to go through every PHP script.

 

Most apache installations will have mod_rewrite installed by default. To check if this module in installed you can verify it by adding following line to the apache configuration file or to an applicable .htaccess file:

 

RewriteEngine On

 

Below are the different methods of setting 301permanent redirection from .htaccess file

 

1. Redirecting Specific Files and Folders from one Domain to Another
This includes redirection from the old server to the new one with the same file name.

Example:

Redirect: http://www.xyz.com/seo/somefile.php
To: /somefile.php

Solution:

RedirectMatch 301 /seo/(.*) /$1

Explanation:

In above expression /seo/(.*) tells apache to match the seo folder followed by zero or more of any characters.  Also ( .*)  in parenthesis tells apache to save the matched string as a back-reference which is placed at the end of the URL that was directed to, in this case it is $1.

 

2. Redirecting Canonical Hostnames

It includes redirecting any requests that do not start with www.abc.org to make sure they included the www. It is done to avoid common canonicalization errors.
Example :

Redirect: http://abc.org/

To: http://www.abc.org/

Redirect: http://mail.abc.org/

To: http://www.abc.org

Redirect: http://abc.org/somefile.php

To: http://www.abc.org/somefile.php

Solution:

RewriteCond %{HTTP_HOST} *!^www*.abc\.org [NC]

RewriteRule (.*) http://www.abc.org/$1 [L,R=301]

Explanation:

The above two expressions tells apache to examine the host the visitor is accessing. The exclamation point (!) in front of www.abc.org negates the comparison, saying, “If the host IS NOT www.abc.org, then perform RewriteRule.” In our case RewriteRule redirects them to www.abc.org while preserving the exact file they were accessing in a back-reference.

 

3. Redirecting Without Preserving the Filename

If you no longer want that all the files that exist on the old server to be also available on the new server then instead of preserving the file names in the redirection which would result in a 404 not found error on the new server, the old files should be redirected to the root URL of the new domain.

Example:

Redirect: http://www.xyz.com/seo/someoldfile.php

To: http://www.abc.org/

Solution:

RedirectMatch 301 /seo/someoldfile.php http://www.abc.org

Explanation:

In above expample all requests for /seo/someoldfile.php is redirected to the root URL of http://www.abc.org.

 

4. Redirecting the GET String

The GET string is the set of characters that come after a filename in the URL and are used to pass data to a web page. Some of the PHP scripts had different names but the GET string stayed the same. So in this case you need to redirect the visitors to the new PHP scripts while preserving GET strings.

An example of a GET string in the URL /myfile.php?this=that&foo=bar would be “?this=that&foo=bar.”

Example:

Redirect: http://www.xyz.com/seo/categorydetail.php?CAT_ID=12345

To: http://www.abc.org/artcat.php?CAT_ID=12345

Solution:

RedirectMatch 301 /seo/categorydetail.php(.*) http://www.abc.org/artcat.php$1

Explanation:

The regular expression above (.*) tells apache to match zero or more of any character and save it as the back-reference $1. Since there is a $1 after /seo/categorydetail.php, it will now redirect the get string to this new PHP file.

 

5. Redirecting While Changing File Extensions

This includes redirecting the files while changing file extensions also. For Example HTML file to PHP file and vice versa.

Example:

Redirect: http://www.xyz.com/seo/guide/anyfile.html

To: http://www.abc.org/articles/anyfile.php

Redirect: http://www.xyz.com/seo/guide/anyfile2.php

To: http://www.abc.org/articles/anyfile2.php

Solution:

RedirectMatch 301 /seo/guide/(.*)\.(php|html) http://www.abc.org/articles/$1.php

Explanation:

(*.) in above expression matches zero or more of any character and saves it as the back-reference $1. \.(php|html) tells apache to match a period followed by either “php” or “html” and saves it as the back-reference $2 (although this isn’t used in this example). Notice the escaped period with a backslash. Enclosing “php” and “html” in parenthesis and separating them with a pipe “|” character means to match either one of the values. For example this (php|html|css|js|jpg|gif) would match any of the files with the extensions php, html, css, js, jpg, or gif.

Related Posts...

GeneralTechnologies