We hope you find this tutorial helpful. In addition to guides like this one, we provide simple cloud infrastructure for developers. Learn more →

How To Create Temporary and Permanent Redirects with Apache and Nginx

PostedAugust 12, 2013 797.4k views Apache Nginx

What is an HTTP Redirect?

HTTP redirection, or URL redirection, is a technique of pointing one domain or address to another. There are many uses for redirection, and a few different kinds of redirection to consider.

As you create content and administrate servers, you will often find the need to redirect traffic from one place to another. This guide will discuss the different use-cases for these techniques, and how to accomplish them in Apache and Nginx.

Why Do Servers Use Redirects?

Redirects are used whenever a site needs people requesting one address to be directed to another address. There are many situations where you may find yourself in this position.

Moving to a Different Domain

If you have established a web presence and would like to change your domain, it is best not to just abandon your old domain.

Bookmarks to your site and links to your site located on other pages throughout the internet will break if your content disappears without any instructions to the browser about how to find its new location.

Changing domains without redirecting will cause your site to lose traffic from previous visitors and lose all of the credibility you have worked to establish.

Expanding to Capture Similar Domains

Often, it is helpful to register multiple variations of a name in order to benefit from users typing in addresses similar to your main domain.

For example, if you have a domain called "mymessyspiders.com", you might also buy the domain names for "mymessyspiders.net" and "mymessyspiders.org" and redirect them both to your "mymessyspiders.com" site.

This allows you to catch users who might be trying to get to your site with the wrong address. It can also help prevent another site from using a similar domain and profiting off of your web presence.

Creating a Persistent Experience In Spite of Page Name Changes

Sometimes, it is necessary to change the names of pages that have already been published and received traffic on your site.

Normally, this would lead to a 404 Not Found error or possibly another error depending on your security settings. These can be avoided by leading your visitors to another page that contains the correct content they were trying to access.

Forcing SSL

A simple but common use for redirects is directing all site traffic to use SSL instead of standard HTTP.

Using redirects, it is possible to make all requests for "http://www.mysite.com" be redirected to "https://www.mysite.com".

Note the HTTPS instead of HTTP.

Redirect Methods

There are a few different kinds of URL redirects, each of which mean something different to the client browser.

The two most common types are 302 temporary redirects, and 301 permanent redirects.

Temporary Redirects

Temporary redirects are useful if your web content for a certain URL temporarily needs to be served out of a different location.

For example, if you are performing site maintenance, you may wish to use a temporary redirect of all of the pages for your domain to an explanation page to inform your visitors that you will be back shortly.

Temporary redirects inform that browser that the content is temporarily located at a different location, but that they should continue to attempt to access the original URL.

Permanent Redirects

Permanent redirects are useful when your content has been moved to a new location forever.

This is useful for when you need to change domains or when the URL needs to change for other reasons and the old location will no longer be used.

This redirect informs the browser that it should no longer request the old URL and should update its information to point to the new URL.

How to Redirect in Apache

Apache can redirect using a few different tools. The simplest ways are accomplished with tools from the "mod_alias" module, but more extensive redirects can be created with "mod_rewrite".

Using the Redirect Directive

In Apache, you can accomplish simple, single-page redirects using the "Redirect" directive, which is included in the "mod_alias" module. This directive takes at least two arguments: the old URL and the new URL.

In its simplest form, you can accomplish a redirect with the following lines in your server configuration:

<VirtualHost *:80>
	ServerName www.domain1.com
	Redirect / http://www.domain2.com
</VirtualHost>

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

This redirect instructs the browser to direct all requests for "www.domain1.com" to "www.domain2.com". This is only for a single page, not for the entire site

By default, the "Redirect" directive establishes a 302, or temporary, redirect.

If you would like to create a permanent redirect, you can do so in either of the following two ways:

Redirect 301 /oldlocation http://www.domain2.com/newlocation
Redirect permanent /oldlocation http://www.domain2.com/newlocation

Using the RedirectMatch Directive

To redirect more than a single page, you can use the "RedirectMatch" directive, which allows you to specify directory matching patterns using regular expressions.

This will allow you to redirect entire directories instead of just single files.

RedirectMatch matches patterns in parenthesis and then references the matched text in the redirect using "$1", where 1 is the first group of text. Subsequent groups are given numbers sequentially.

For example, if we wanted to match every request for something within the "/images" directory to a subdomain named "images.example.com", we could use the following:

RedirectMatch ^/images/(.*)$ http://images.example.com/$1

As with the "Redirect" directive, you can specify the type of redirect by adding the redirect code before the URL location rules.

Using mod_rewrite to Redirect

The most flexible, but complicated way to create redirect rules is with the module called "mod_rewrite".

This is outside of the scope of this article, but you can learn how to use mod_rewrite in this article.

How to Redirect in Nginx

Redirects in Nginx are in many ways much simpler to accomplish. Most of the times, you can redirect by creating a server block for the content you would like to redirect.

For example, if you would like to redirect requests for "domain1.com" to "domain2.com", you could create a server block that looks like this:

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

The "return" directive executes a URL substitution and then returns the status code given to it and the redirection URL.

In this case, it uses the "$scheme" variable to use whatever scheme was used in the original request (http or https). It then returns the 301 permanent redirect code and the newly formed URL.

In order to process a folder redirect to a separate subdomain, we can perform an operation similar to the Apache folder redirection using the "rewrite" directive:

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

This directive, when placed in a server block, will issue a temporary redirect for requests inside the "images" directory to the subdomain "images.example.com".

For a permanent redirect, you could change "redirect" to "permanent" at the end of the statement.

Conclusion

You now have the knowledge to redirect your request to new locations. Be sure to use the correct redirection type, as an improper use of temporary redirects can hurt your search ranking.

Using redirects correctly will allow you to leverage your current web presence while allowing you to modify your site structure as necessary. If you would like to learn more about the ways that you can redirect your vistors, both Apache and Nginx have great documentation on the subject.

By Justin Ellingwood

25 Comments

Creative Commons License