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:
1sudo systemctl restart httpd</pre
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 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 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 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 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 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.