Quantcast
Channel: Tech Support
Viewing all articles
Browse latest Browse all 880

How To Set Up Temporary and Permanent Redirects in Apache

$
0
0

There are a several different types of redirects but the two most common types are temporary and permanent redirects. This article will guide you through the steps to create temporary redirect and permanent redirect in Apache web server.






Temporary redirects are useful if a URL temporarily needs to be served from a different server or location. For instance, if you are performing site maintenance, you may wish to create a temporary redirect of from your domain to an explanation page to inform your visitors that the original website will be back shortly.

Permanent redirects are useful when your content has been permanently moved to a new location, like when you change domain names.

You can create a temporary redirect in Apache by adding a line like this to the virtual host entry in the server configuration file:


Redirect/oldlocationhttp://www.yourdomain.com/newpage
Similarly, use a line like this for a permanent redirect:
Redirect permanent /oldlocationhttp://www.yourdomain.com/newpage
In its simplest form, you can accomplish a temporary redirect with the following lines in your server configuration:
Temporary redirect with Redirect

ServerName www.yourdomain1.com
Redirect / http://www.yourdomain2.com
ServerName www.yourdomain2.com . . . This redirect instructs the browser to direct all requests for www.yourdomain1.com to www.yourdomain2.com. This solution, however, works only for a single home page, not for the entire site.

For example, if you wanted to temporarily redirect every page within www.yourdomain1.com to www.yourdomain2.com, you could use the following:
Temporary redirect with RedirectMatch

ServerName www.yourdomain1.com
RedirectMatch ^/(.*)$ http://www.yourdomain2.com/$1
ServerName www.yourdomain2.com . . .
By default, both Redirect and RedirectMatch directives establish a temporary redirect. If you would like to create a permanent redirect, you can do so by appending permanent to either of the directives:
Permanent redirects
Redirectpermanent / http://www.yourdomain2.com
RedirectMatchpermanent ^/(.*)$ http://www.yourdomain2.com/$1
Let's assume you have your website configured to be served from a single domain called yourdomain1.comalready configured in Apache as follows:
/etc/apache2/sites-available/yourdomain1.com.conf

ServerAdmin admin@yourdomain1.com
ServerName yourdomain1.com
ServerAlias www.yourdomain1.com
DocumentRoot /var/www/yourdomain1.com/public_html
ErrorLog${APACHE_LOG_DIR}/error.log
CustomLog${APACHE_LOG_DIR}/access.log combined

Moving to a Different Domain

We'll also assume you are already serving your future version of website at yourdomain2.com:
/etc/apache2/sites-available/yourdomain2.com.conf

ServerAdmin admin@yourdomain2.com
ServerName yourdomain2.com
ServerAlias www.yourdomain2.com
DocumentRoot /var/www/yourdomain2.com/public_html
ErrorLog${APACHE_LOG_DIR}/error.log
CustomLog${APACHE_LOG_DIR}/access.log combined
Let's change the yourdomain1.com virtual host configuration file to add a permanent redirect to yourdomain2.com:
/etc/apache2/sites-available/yourdomain1.com.conf

ServerAdmin admin@yourdomain1.com
ServerName yourdomain1.com
ServerAlias www.yourdomain1.com
DocumentRoot /var/www/yourdomain1.com/public_html
ErrorLog${APACHE_LOG_DIR}/error.log
CustomLog${APACHE_LOG_DIR}/access.log combined
RedirectMatch permanent ^/(.*)$ http://yourdomain2.com/$1
We've added the aforementioned redirect using a RedirectMatch directive. We use RedirectMatchinstead of a simple Redirect to make sure that all website pages will get affected, not only the home page. The ^/(.*)$ regular expression matches everything after the / in the URL. For example, http://yourdomain1.com/index.html will get redirected to http://yourdomain2.com/index.html. To achieve the permanent redirect we simply add permanent after the RedirectMatch directive.


Creating a Persistent Experience Despite Single Page Name Changes

Suppose your website had two separate pages for products and services called products.html and services.html respectively. Now, you've decided to replace those two pages with a single offer page called offers.html instead. We will configure a simple redirect for products.html and services.html to offers.html.
We assume you have your website configured as follows:
Assumed original virtual host configuration

ServerName example.com
. . .
Configuring the redirects is as simple as using two Redirect directives.
Redirects added to the original configuration

ServerName example.com

Redirect permanent /products.html /offer.html
Redirect permanent /services.html /offer.html
. . .
The Redirect directive accepts the original address that has to be redirected as well as the destination address of a new page. Since the change here is not a temporary one, we used permanent in the directive as well. You can use as many redirects like that as you wish to make sure your visitors won't see unnecessary Not Found errors when moving site contents.







Conclusion

You now have the basic knowledge to redirect requests to new locations or page. Be sure to use the correct redirection type. There are multiple other uses of HTTP redirects, including forcing secure SSL connections (i.e. using https instead of http) and making sure all visitors will end up only on the www. prefixed address of the website.

Viewing all articles
Browse latest Browse all 880

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>