URLs Redirect with Apache Web Server

URLs Redirect with Apache Web Server

You can learn how to redirect URLs with Apache in this section. Redirecting a URL allows you to return an HTTP status code that guides the client to a specific URL, making it useful for instances where a piece of content has been transferred. Redirect is a member of the mod alias module of Apache.

Before You Begin

1. This guide assumes you have followed our Guides to Getting Started and Securing Your Server and you have installed your Apache installation already. If you have not, please refer to our Apache or LAMP stack guides.

2. You can change the configuration files for Apache in this guide so make sure you have the correct permissions to do so.

3. Update your system.

Based on the distribution of Linux, the Apache virtual host configuration files are located at various locations. CentOS 7, for example: /etc/httpd /conf.d/vhost.conf; Ubuntu 16.04: /etc/apache2/sites-available/abc.com.conf. For example. In order to be brief, the configuration file extracts from this guide will lead you to the configuration option for Apache.

After making changes, remember to reload Apache configuration:

CentOS 7

sudo systemctl restart httpd

Ubuntu 16.04

sudo systemctl restart apache2

The Redirect Directive

You can find Redirect settings in your main Apache configuration file but we suggest that you keep them in your virtual host files or directory blocks. You can also use the Redirect statements in files with .htaccess. Here is an example of how Redirect could be used:

Apache configuration option

If no reason is provided, Redirect will submit a temporary (302) status code, and the client will be told that the /user name resource has temporarily moved to http:/team.abc.com/~user/.

No matter where they are located, Redirect statements must define the redirected resource’s complete file path, following the domain name. Such statements must provide even the full URL of the new location of the site.

You may also have a claim to return a HTTP status specific to:

Apache configuration option

Redirect permanent /username http://team.abccom/~username/
Redirect temp /username http://team.abc.com/~username/
Redirect seeother /username http://team.abc.com/~username/
Redirect gone /username

  • Permanent informs the customer the tool has permanently shifted. That returns a status code of 301 HTTP.
  • Temp is the default action, which informs the client that the resource has temporarily relocated. This returns a 302 HTTP status code.
  • Seeother informs the user that the requested tool was replaced with a different one. This returns a 303 HTTP status code.
  • Gone informs the user that they have permanently removed the tool they are searching for. You don’t need to define a definite URL when using this statement. That returns a status code of 410 HTTP.

The HTTP status codes can also be used as claims. Here is an example using the options with the status code:

Apache configuration option

Redirect 301 /username http://team.abc.com/~username/
Redirect 302 /username http://team.abc.com/~username/
Redirect 303 /username http://team.abc.com/~username/
Redirect 410 /username

RedirectPermanent and RedirectTemp can also execute permanent and temporary redirects, respectively:

Apache configuration option

RedirectPermanent /username/bio.html http://team.abc.com/~username/bio/
RedirectTemp /username/bio.html http://team.abc.com/~username/bio/

Redirects can also be achieved using the regex patterns, using RedirectMatch:

Apache configuration option

RedirectMatch (.*).jpg$ http://static.abc.com$1.jpg

This match every request for a file with an extension of .jpg and replaces it with a position on a domain. The parentheses allow you to get a specific part of the request and insert it as a variable (specified by $1$2, etc.) into the URL of the new site. For instance:

Beyond URL Redirection

Apache also helps you to edit URLs using mod rewrite, in addition to redirecting users. Although the features are identical, the key difference being that rewriting a URL requires the server returning a request other than the one received by the client, while redirecting simply returns a status code, and the client then asks for the “correct” answer.

Rewriting a request on a more realistic level does not affect the contents of the browser’s address bar, and can be useful in hiding URLs with sensitive or vulnerable data.

Even though redirection helps you to quickly adjust the positions of different tools, in some cases you can find that rewriting suits your needs better. See Apache’s mod rewrite documentation for more detail.

Thankyou..

    • Related Articles

    • Rewrite URLs using mod_rewrite and Apache

      You’ll learn how to rewrite the URLs using mod rewrite and Apache in this article. Rewrite a URL is a server-side operation which can serve contents from a position of a file system that does not exactly suit the client’s request. It could be helpful ...
    • Tuning Of Your Apache Server

      Your Apache configuration settings have a significant impact on the output of your Microhost Cloud server. There are several tools that can be used to further examine the performance of your Apache server and make educated choices on how to start ...
    • Managing Resources using Apache mod_alias

      In certain cases all of the services that an Apache host uses are stored in the DocumentRoot of that server. The DocumentRoot is a directory listed in the block of configuration for <VirtualHost>. This directory is intended to represent the various ...
    • How to Set Up the htaccess File in Apache

      Introduction The purpose of this guide is to show you how to configure Apache htaccess (.htaccess) configuration. The guide covers subjects related to website file system permissions, redirects and limitations of IP address. Before You Begin This ...
    • Basics Information of Apache Configuration

      The Apache HTTP web server is the de facto standard for general HTTP services in many respects. Thanks to its large number of modules, it offers versatile support for proxy servers, URL rewriting, and granular access control. In addition, web ...