November 26, 2020

How to redirect URL on Apache web server

2 min read
When HTTP resources or web pages change location, it is often important to provide certain methods to remind users that these resources have moved. The HTTP protocol provides multiple “redirection” status codes for communicating with client applications for this purpose without affecting the user experience.
Apache provides many “redirect” configuration instructions that allow administrators to specify resources in the configuration file to redirect to another URL. When the request is redirected, the server returns the result of the request, which instructs the client to initiate a second request for the new location of the target resource.
Redirection can tell the client that the requested page has moved temporarily or permanently. Apache provides tools to easily support these functions. This guide describes redirection configuration instructions, how to set various redirection options, and how to redirect resource request classes to a new location.
Apache Pulsar
Redirect
Redirection configuration directives can be located in the master server configuration file, but we recommend that you keep them in virtual host entries or directory blocks. You can also declare redirect statements in the .httaccess file. The following is an example of the Redirect instruction:
Redirect /old_direct http://example.com/new_direct/
If no parameters are given, a temporary (eg 302) redirection status is sent. In this case, the client (user agent) is notified that the available resources at //old_direct have been temporarily moved to http://example.com/new_direct/.
To specify a specific HTTP redirect status, specify one of the following statuses:
Redirect permanent /username http://example.com/~username/
Redirect temp /username http://example.com/~username/
Redirect seeother /username http://example.com/~username/
Redirect gone /username
This redirect tells the client that the resource has moved permanently, which corresponds to the HTTP status 301. The “temp” status is the default behavior, specifying that the redirect is only temporary; this corresponds to the HTTP status 302. The “another” status is sent to indicate the requested Signal that the resource has been replaced by another resource (HTTP status 303). Finally, the “gone” status tells the client that the resource has been deleted (permanently); this sends the HTTP status 410 as an alternative to the unavailable “404” status. In the case of a “leaved” redirect, please ignore the final URL.
Apache also provides two additional permanent and temporary redirection instructions, which are clearer. They are as follows:
RedirectPermanent /username/bio.html http://example.com/~username/bio/
RedirectTemp /username/bio.html http://example.com/~username/bio/
In addition, Apache can also use the RedirectMatch instruction to use regular expressions to redirect a type of request to a new address. E.g:
RedirectMatch (.*)\.jpg$ http://static.example.com$1.jpg
This directive matches any request for a file with a .jpg extension and replaces it with a location on the second domain. therefore:
Request for http://www.example.com/avatar.jpg will be redirected to http://static.example.com/avatar.jpg
Requests for http://www.example.com/images/avatar.jpg will be redirected to http://static.example.com/images/avatar.jpg.