Official Power Up Hosting Blog

Everything about Linux, Windows, and hosting ;)

I am an avid book reader, who enjoys technology as well as writing.

Share


Our Newsletter


Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Tags


Twitter


Official Power Up Hosting Blog

How To Create Temporary and Permanent Redirects with Apache

Apurva ChodnekarApurva Chodnekar

Introduction:

When you put the word temporary or permanent in front of a word or a term, the nature of that term is a foregone conclusion.

We are here to learn how to create temporary and permanent redirects with Apache.

And we will get to it shortly, but before that let's take a look at temporary and permanent redirects and what they actually mean.

Temporary redirects:

Temporary redirect is denoted by a htaccess redirect 302 status code.

These redirects stay true to the term temporary and provide a temporary solution to websites or web pages that are under construction or need to be redirected for a limited period of time.

What's basically done is apache redirect url to another url i.e we can redirect our old url to a temporary new url until the site is back and running.

It's about redirecting the traffic of your website or webpage to the new and temporary url for the time being.

A simple way of adding redirect in apache for a single web page is by adding the following line in the virtual host block in the server configuration file.

Redirect /old-page/ http://www.domainname.com/new-page

Here we are redirecting the new-page to the old-page. So when a user tries to access the new-page he/she will be redirected to the old page.

Permanent redirects:

A permanent redirect is denoted by the 301 redirect htaccess code.

As the name suggests they are permanent. It's redirecting an old domain to a new domain one.

Changing your domain name should be a last resort. Do it only if you absolutely have to.

With apache redirect 301 you can manage to retain some of your followers if you decided to move your website to a new domain.

Without a permanent redirect, you would have to start from scratch.

It's mostly used when someone wants to move their website to a new domain name or delete a web page or website.

For example, if a product has been discontinued, you will have to take it down from your website.

Instead of deleting the page you can add a permanent redirect to similar products on your website.

In order to redirect permanent https we need to add the below line in the virtual host block of the server configuration file.

Redirect permanent /old-page/ http://www.domainname.com/new-page

It's similar to temporary redirect you just have to add the word permanent after the Redirect directive.

This line will redirect the new-page from www.domainname.com to the old-page of the same website.

This was just a little taste of what's to come ahead. The redirect directive we used can only redirect one web page to another.

We'll see two scenarios which will help you understand how to create temporary and permanent redirects with apache in detail.

Prerequisites:

A server with Apache 2 installed. It should be set up to serve your website or websites with virtual hosts.

Scenario 1 :
Moving your website to a different domain.

This scenario will help you create redirects with apache if you want to move your website to a different domain.

Let's assume that you have a well-established website.
And you have decided to move it to a new domain.

If you simply move the website you will lose followers and you will have to start from the scratch.

So, what you could do is redirect the domain. apache redirect domain is quite simple once you get the hang of it. This way, you will be able to keep your followers/clients i.e the traffic flow of your website will not suffer.

We will consider firstdomain.com as the old domain of a website and the seconddomain.com as the new domain.

Below code shows the configuration of the firstdomain.com configuration file in apache.

/etc/apache2/sites-available/firstdomain.com.conf

<VirtualHost *:80> ServerAdmin admin@firstdomain.com ServerName firstdomain.com ServerAlias www.firstdomain.com DocumentRoot /var/www/firstdomain.com/public_html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>

The next code block shows the configuration of the seconddomain.com configuration file in apache.

/etc/apache2/sites-available/seconddomain.com.conf

<VirtualHost *:80> ServerAdmin admin@seconddomain.com ServerName seconddomain.com ServerAlias www.seconddomain.com DocumentRoot /var/www/seconddomain.com/public_html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>

We will now change the firstdomain.com virtual host configuration file by adding a permanent redirect to seconddomain.com.

/etc/apache2/sites-available/firstdomain.com.conf

<VirtualHost *:80> ServerAdmin admin@firstdomain.com ServerName firstdomain.com ServerAlias www.firstdomain.com DocumentRoot /var/www/firstdomain.com/public_html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined RedirectMatch permanent ^/(.*)$ http://seconddomain.com/$1 </VirtualHost>

As you can see, we have used RedirectMatch directive instead of a Redirect directive.

That's because with Redirect you can only direct a single web page to another web page.

RedirectMatch, on the other hand, is used to redirect one domain to another domain. It uses regular expression i.e. ^/(.*)$ to match the everything that comes after the / in the URL.

That is URL http://firstdomain.com/inform.html will be directed to http://seconddomain.com/inform.html.

It matches the web pages in the domain and directs them to the respective web pages.

And we have already discussed for permanently redirecting the web pages or a domain we have to add the word permanent after Redirect or RedirectMatch.

Scenario 2: When you change the name of web pages.

In time you have to change the name of web pages of your site, the reasons for which may vary from person to person.

When you change the name of one of the pages on your website do not forget to redirect it.

People might have bookmarked the page and if you don't remember to redirect it they will not find your website.

And that will lead to loss of followers and reduced traffic.

In order to explain this scenario let's take an example. Consider that your website has two separate web pages, dealoftheday.html, and discounts.html.

But you have had a better idea and want to incorporate these two pages into a single, offers.html web page.

Consider the following as the configuration of shopitwild.com domain.

<VirtualHost *:80> ServerName Shopitwild.com . . . </VirtualHost>

To redirect dealoftheday.html and discounts.html to offers.html we'll have to add two Redirect directives to the above configuration file.

After adding the Redirect directives the configuration file will look somewhat like that.

<VirtualHost *:80> ServerName Shopitwild.com Redirect permanent /dealoftheday.html /offers.html Redirect permanent /discounts.html/offers.html . . . </VirtualHost>

Conclusion:

These two are the most likely scenarios that you will have to deal with.

But now that you know the difference between directives as well temporary and permanent redirect it will be easier for you to redirect requests to new locations(web pages or websites).

The code for temporary apache redirect https to https is simple enough as we have already seen.

I am an avid book reader, who enjoys technology as well as writing.

Comments