URL Redirect: 301 & 302 Redirects

429 0
301 & 302 URL Redirect

Table of Contents

URL Redirect Methods

URL redirects are a very important part for every webmaster. There are a couple of ways to use URL redirects, each of them has their own advantages and meaning to the browser at the client side. Here is the list of two most common URL redirect type:

  • 302 redirects It’s a temporary URL redirect.
  • 301 redirect: It’s a permanent URL redirect).

1. Temporary Redirects

Temporary URL redirect i.e., 302 redirect means that the resource that client browser is requesting is temporarily available at some different location. So once the resource location changes for some time, we need to specify the new location using this URL redirect method. So that when client browser requests for that particular resource, it gets directed to the new location.

If you are in need to perform maintenance of your website, then, in that case, you can temporarily redirect all the pages of your website to a particular page that you specially designed to let the users know that your website will be online shortly after maintenance.

2. Permanent Redirect

Permanent URL Redirect i.e., 301 redirect means that the resource(s) is/are moved permanently to some new location. This kind of URL redirect at the client side informs the browser that old URL is no longer exists. So in that case browser at the client side should not request the old URL & should point to the new URL for that particular resource.

One of the best examples for the permanent redirect is a change of domain name. If you are planning to change your domain. Then, in that case, you would want to retain your audience to your new domain. So you can use the permanent redirect method to tell you, users, that the old domain no longer exists or this website is moved to a new place with a new name.

How to Redirect in Apache

The simplest way of temporary redirection can be accomplished by using the “mod_alias” module. For more extensive redirects you can use."mod_rewrite"

a. URL Redirect using the Redirect Directive

URL redirect in Apache can be accomplished by using the "Redirect" directive. This "Redirect" directive is included in the module"mod_alias". This Redirect directive takes two arguments, the old URL & the new URL. Here’s how redirection can be accomplished:

<VirtualHost *:80>
ServerName www.example1.com
Redirect / http://www.example2.com
</VirtualHost>

<VirtualHost *:80>
ServerName www.example2.com
. . .
. . .
</VirtualHost>

b. URL Redirect by using RedirectMatch Directive

In case you want to redirect more than one web page, then you can use "RedirectMatch" directive. RedirectMatch works in the same manner as the RedirectDirective does, but it uses a regular expression instead of a prefix as the source URL.

For example, you can use the following line of code to redirect all GIF images requests to another server:

RedirectMatch (.*)\.gif$ http://www.example-site.com$1.gif

URL Redirect in Nginx

Accomplishing redirects in Nginx is much easier. It can be accomplished by creating server block for the resource that you would want to redirect. Here are some of the examples:

a. Redirect Domain

If you want to redirect one domain to another. Like “example1.com” to “example2.com”, then you can create a server block inside your configuration that looks like something this:

server {
listen 80;
server_name example1.com;
return 301 $scheme://example2.com$request_uri;
}

b. Redirect Non-www to www Redirect

If you want to redirect Non-www to www, then you can use the following configuration:

server {
server_name example.com;
rewrite ^/(.*)$ http://www.example.com/$1 permanent;
}

c. Redirect www to Non-www Redirect

In case you want to redirect from Non-www to www, then you can use the following configuration:

server {
server_name www.example.com;
rewrite ^/(.*)$ http://example.com/$1 permanent;
}

d. Single Page 301 Redirect

In case you want to redirect a single page to some other pages just to avoid 404 errors, you can use the code given below, inside the server block:

if ($request_filename ~ oldpage/) {
rewrite ^ http://www.example.com/newpage/? permanent;
}

e. Directory 301 Redirect

The configuration below can help you to redirect your old website directory to a new website directory:

if ($request_filename ~ olddirectory/.+ ) {
rewrite ^(.*) http://www.example.com/newdirectory/$1 permanent;
}

f. Domain to Domain 301 Redirect with Posts

If you have changed the domain and looking to redirect your old domain to a new one, the following configuration can be used inside your server block:

server {
server_name example.com www.example.com;
rewrite ^ $scheme://www.newdomain.com$request_uri permanent;
}

g. Domain to Domain 301 Redirect

Domain to domain 301 redirect is same as above but the only difference it has that it does not redirect page request to another page of the domain. This kind of redirect is used when you are looking to redirect your website to the homepage of your another domain.

server {
server_name example.com www.example.com;
rewrite ^ $scheme://www.newdomain.com;
}

h. Folder redirect to a sub-domain

In case you want to process the folder redirect to a sub-domain, you can use “rewrite” directive method to perform this redirect. Here’s how it can be done:

rewrite ^/images/(.*)$ http://images.example.com/$1 redirect;

Conclusion

Now you must be knowing how to use URL redirect feature. But before you choose any kind of  URL redirect to make sure that you are using the right type of  URL redirect. Any wrong or improper use of URL redirects can hurt your search engine ranking.

Hope you will like this post! Subscribe to our newsletter to get weekly updates and do not forget to share this post on social media.

In this article


Join the Conversation


URL Redirect: 301 & 302 Redirects

URL Redirect Methods. There are a few different kinds of URL redirects, each of which has different meaning to the client browser. The two most common types

Send this to a friend