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/ 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
Redirect /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

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
Redirect permanent /username
Redirect temp /username
Redirect seeother /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
Redirect 301 /username
Redirect 302 /username
Redirect 303 /username
Redirect 410 /username

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

Apache configuration option
RedirectPermanent /username/bio.html
RedirectTemp /username/bio.html

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

Apache configuration option
RedirectMatch (.*)\.jpg$$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 will be redirected to and
  • A request for will be redirected to

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.

comments powered by Disqus

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