Redirect URLs with the Apache Web Server

Updated by Phil Zona Written by Linode

Contribute on GitHub

Report an Issue | View File | Edit File

In this guide, you’ll learn how to redirect URLs with Apache. Redirecting a URL allows you to return an HTTP status code that directs the client to a different URL, making it useful for cases in which you’ve moved a piece of content. Redirect is part of Apache’s mod_alias module.

Redirect URLs with the Apache Web Server

Before You Begin

  1. This guide assumes you have followed our Getting Started and Securing Your Server guides, and that you have already configured your Apache installation. If you haven’t, refer to our Apache guides or LAMP stack guides.

  2. In this guide, you will modify the Apache configuration files, so be sure you have the proper permissions to do so.

  3. Update your system.

The Apache virtual host configuration files are found in different places, depending on the distribution of Linux. For example, on CentOS 7: /etc/httpd/conf.d/vhost.conf; on Ubuntu 16.04: /etc/apache2/sites-available/example.com.conf. For the sake of brevity, configuration file excerpts in this guide will direct you to Apache configuration option.

Remember to reload Apache configuration after making changes:

CentOS 7
1sudo systemctl restart httpd
Ubuntu 16.04
1sudo systemctl restart apache2

The Redirect Directive

Redirect settings can be located in your main Apache configuration file, but we recommend you keep them in your virtual host files or directory blocks. You can also use Redirect statements in .httaccess files. Here’s an example of how to use Redirect:

Apache configuration option
1
Redirect /username http://team.example.com/~username/

If no argument is given, Redirect sends a temporary (302) status code, and the client is informed that the resource available at /username has temporarily moved to http://team.example.com/~username/.

No matter where they are located, Redirect statements must specify the full file path of the redirected resource, following the domain name. These statements must also include the full URL of the resource’s new location.

You can also provide an argument to return a specific HTTP status:

Apache configuration option
1
2
3
4
Redirect permanent /username http://team.example.com/~username/
Redirect temp /username http://team.example.com/~username/
Redirect seeother /username http://team.example.com/~username/
Redirect gone /username
  • permanent tells the client the resource has moved permanently. This returns a 301 HTTP status code.
  • temp is the default behavior, and tells the client the resource has moved temporarily. This returns a 302 HTTP status code.
  • seeother tells the user the requested resource has been replaced by another one. This returns a 303 HTTP status code.
  • gone tells the user that the resource they are looking for has been removed permanently. When using this argument, you don’t need to specify a final URL. This returns a 410 HTTP status code.

You can also use the HTTP status codes as arguments. Here’s an example using the status code options:

Apache configuration option
1
2
3
4
Redirect 301 /username http://team.example.com/~username/
Redirect 302 /username http://team.example.com/~username/
Redirect 303 /username http://team.example.com/~username/
Redirect 410 /username

Permanent and temporary redirects can also be done with RedirectPermanent and RedirectTemp, respectively:

Apache configuration option
1
2
RedirectPermanent /username/bio.html http://team.example.com/~username/bio/
RedirectTemp /username/bio.html http://team.example.com/~username/bio/

Redirects can be made with regex patterns as well, using RedirectMatch:

Apache configuration option
1
RedirectMatch (.*)\.jpg$ http://static.example.com$1.jpg

This matches any request for a file with a .jpg extension and replaces it with a location on a given domain. The parentheses allow you to get a specific part of the request, and insert it into the new location’s URL as a variable (specified by $1, $2, etc.). For example:

  • A request for http://www.example.com/avatar.jpg will be redirected to http://static.example.com/avatar.jpg and
  • A request for http://www.example.com/images/avatar.jpg will be redirected to http://static.example.com/images/avatar.jpg.

Beyond URL Redirection

In addition to redirecting users, Apache also allows you to rewrite URLs with mod_rewrite. While the features are similar, the main difference is that rewriting a URL involves the server returning a different request than the one provided by the client, whereas a redirect simply returns a status code, and the “correct” result is then requested by the client.

On a more practical level, rewriting a request does not change the contents of the browser’s address bar, and can be useful in hiding URLs with sensitive or vulnerable data.

Although redirection allows you to easily change the locations of specific resources, you may find that rewriting better fits your needs in certain situations. For more information, refer to Apache’s mod_rewrite documentation.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

Join our Community

Find answers, ask questions, and help others.

This guide is published under a CC BY-ND 4.0 license.