How to redirect URL on Apache web server

When HTTP resources or web pages change location, it is often important to provide certain methods to alert users that these resources have moved. The HTTP protocol provides multiple "redirection" status codes for this purpose, which are used to communicate with the client application 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. After redirecting the request, the server will return the request result, 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.

Redirect

The redirect configuration directive can be located in the main server configuration file, but we recommend that you keep it in the virtual host entry or directory block. You can also declare redirect statements in .httaccess files. The following are examples of redirect instructions:

To specify a specific HTTP redirect status, specify one of the following statuses:

This redirect tells the client that the resource has moved permanently, which corresponds to HTTP status 301. The "temp" state is the default behavior, indicating that the redirection is only temporary; the default state is redirection. This corresponds to HTTP status 302. An "another" status is sent to indicate the requested signal: the resource has been replaced by another resource (HTTP status 303). Finally, the "gone" status tells the client that the resource has been (permanently) deleted; this sends HTTP status 410 as an alternative to the unavailable "404" status. If it is a "leaved" redirect, please ignore the final URL.

Apache also provides two other permanent and temporary redirect instructions, which are more clear. They are as follows:

In addition, Apache can also use the RedirectMatch directive to use regular expressions to redirect the request type to a new address. E.g:

This directive matches any request for a file with a .jpg extension and replaces it with a location on the second domain. therefore:

Guess you like

Origin www.linuxidc.com/Linux/2020-04/162882.htm