How To Create Temporary and Permanent Redirects with Apache and Nginx

How To Create Temporary and Permanent Redirects with Apache and Nginx

How To Create Temporary and Permanent Redirects with Apache and Nginx

Redirects are an important tool for website administrators to manage changes to website URLs. They allow you to send visitors to a different page or website, either temporarily or permanently. In this tutorial, we'll explore how to create temporary and permanent redirects using Apache and Nginx.

Temporary Redirects with Apache

To create a temporary redirect with Apache, you'll need to modify your .htaccess file. Add the following code to your file:

      RewriteEngine on
      RewriteRule ^old-page$ /new-page [R=302,L]
    

This code will redirect visitors from http://example.com/old-page to http://example.com/new-page with a HTTP status code of 302. This tells search engines that the redirect is temporary and that they should continue to index the original page.

Permanent Redirects with Apache

For a permanent redirect, use the following code:

      RewriteEngine on
      RewriteRule ^old-page$ /new-page [R=301,L]
    

This code will redirect visitors from http://example.com/old-page to http://example.com/new-page with a HTTP status code of 301. This tells search engines that the redirect is permanent and that they should update their index to reflect the new URL.

Temporary Redirects with Nginx

To create a temporary redirect with Nginx, you'll need to modify your server block configuration file. Add the following code to your file:

      location /old-page {
        return 302 /new-page;
      }
    

This code will redirect visitors from http://example.com/old-page to http://example.com/new-page with a HTTP status code of 302.

Permanent Redirects with Nginx

For a permanent redirect, use the following code:

      location /old-page {
        return 301 /new-page;
      }
    

This code will redirect visitors from http://example.com/old-page to http://example.com/new-page with a HTTP status code of 301.

Keywords: HTML, tutorial, temporary redirects, permanent redirects, Apache, Nginx, .htaccess file, server block configuration file, HTTP status code.

Комментарии

Популярные сообщения из этого блога

How To Modify CSS Classes in JavaScript

How To Backup MySQL Databases on an Ubuntu VPS

How To Backup PostgreSQL Databases on an Ubuntu VPS