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 “”, you might also buy the domain names for “” and “” and redirect them both to your “” 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 “” be redirected to “”.

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>
 Redirect /
<VirtualHost *:80>
 . . .
 . . .
 This redirect instructs the browser to direct all requests for "" to "". 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
Redirect permanent /oldlocation

Redirect non www. to www.

If you would like to simply redirect your non-www to your www domain then simply define it as a virtual host then have another virtual host defined with your real config. This is a lighter weight solution than using mod_rewrite.

<VirtualHost *:80>
 Redirect permanent /

If you really want to use one vhost config to accomplish this, you can. The answer is the very first word of this paragraph. You can put the IF statement inside of your real vhost config.

<If "req('Host') != ''"
Redirect "/" ""

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 “”, we could use the following:

RedirectMatch ^/images/(.*)$$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 “” to “”, you could create a server block that looks like this:

server {
 listen 80;
 return 301 $scheme://$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/(.*)$$1 redirect;

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

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


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.

Page generated in 0.4547 seconds.
Advertisment ad adsense adlogger