Open In App

How to Add Redirects in Apache?

Last Updated : 12 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The redirects are one of the most important configurations that allow a web server to redirect traffic from one URL to another. This would help you forward visitors to a new location whenever content changes its location, or it will let you show multiple URLs under one canonical URL. In this article, we'll see how to add redirects in Apache—the

Types of Redirects in Apache

  • 301 Permanent Redirect: The work of this redirect is to tell the browser that the page is removed to the another URL permanently.
  • 302 Redirect: This will indicate to the Search Engines that this is temporary in nature, and that the old URL might be turned back on.
  • 303 See Other: This is for redirecting after a POST. The client should perform a GET for some other resource.
  • 307 Redirect (Temporary) Like 302, but preserves the HTTP method.

These are the following methods to add redirects:

Configuring Redirects in Apache

Well, Apache provides several ways of setting up redirects based on your needs and server setup.

Using a .htaccess File

  • This is the most common method of implementing redirects onto shared hosting environments.
  • Now you have to create a .htccess file in case you have it already then you don't need to create it.

301 Redirect Example:

Redirect 301 /old-page.html http://www.example.com/new-page.html

302 Redirect Example:

Redirect 302 /temporary-page.html http://www.example.com/new-temporary-page.html

Using the Apache Configuration Files

  • You can add redirects directly into the VirtualHost configuration if you have access to the server's main configuration files, such as httpd.conf or apache2.conf.
  • Find and open your Apache configuration file. It could be httpd.conf or apache2.conf.

Add Redirect Rule within the VirtualHost Block:

301 Redirect Example:

<VirtualHost *:80>
ServerName www.example.com
Redirect 301 /old-page.html http://www.example.com/new-page.html
</VirtualHost>

Using the mod_rewrite Module

  • The mod_rewrite module gives greater flexibility and power for redirects, particularly where complex URL patterns are involved.
  • Enable mod_rewrite: Make sure that mod_rewrite module is turned on in your Apache server.

You can turn it on with the command:

sudo a2enmod rewrite

Add Rewrite Rules in .htaccess or Apache Config:

301 Redirect Example:

RewriteEngine On
RewriteRule ^old-page\.html$ http://www.example.com/new-page.html [R=301,L]

Conclusion

Setting up redirects in Apache is one of the fundamental skills to run the traffic on a website. Be it condensing pages, handling moved content, or optimizing the site for search engines; proper use of redirects guarantees that both users and search engines could always find your content. Now, by following these steps, you can go about confidently using the different methods that Apache provides to implement redirects.


Next Article
Article Tags :

Similar Reads