22.08.2023

How to set up redirect via .htaccess?

Introduction

Redirects serve as essential tools for company websites, offering numerous benefits for effective management:

In a nutshell, redirects are indispensable for maintaining seamless user experiences, upholding SEO rankings, and deftly managing shifts in a company's website structure, content, and URLs. They serve as custodians of brand credibility, website user—friendliness, and overall digital prosperity.

Requirements

Installation

First of all we need to update all packages in the system and install them:

apt update && apt upgrade -y

Screenshot №1 — Update

If you already have web—server with html or php—page then skip that stage, for other case we setup web—server step—by—step. We need to download web—server in our example that woudl be Apache. Type the command below:

apt install apache2

Screenshot №2 — Installation

Let's configure environment for settings web—server, open the main configuration file:

nano /etc/apache2/apache2.conf

And changes all parameters AllowOverride to value All, that needed for properly work .htaccess files, which we will create in worker directory of site. Then press combination of Ctrl+O and Ctrl+X for save and exit. Create worker directory for site and two html pages for redirecting:

mkdir /etc/test

By the command below make a html page for start redirect:

nano /etc/test/site1.html

Enter that HTML—code below or use your own page:

<! DOCTYPE html>
<html>
<head>
<title>FIRST PAGE</title>
</head>
<body>
<h1>That starting page!</h1>
</body>
</html> 

Screenshot №3 — First HTML

By the command below make a html page for target site:

nano /etc/test/site2.html

Enter that HTML—code below or use your own page:

<! DOCTYPE html>
<html>
<head>
<title>SECOND PAGE</title>
</head>
<body>
<h1>Congratulations, you are in target site!</h1>
</body>
</html>

Screenshot №4 — Second HTML

After we create page for our manipulation, we can go to the settings of the site by the command  below:

nano /etc/apache2/sites-available/000-default.conf

And edit accordance parameters for properly work, indicate DocumentRoot as folder we created before, ServerName in that field enter domain name and in the last for your directory changes AllowOverride to All! Ctr+O and Ctrl+X for saving and exit of file:

Screenshot №5 — Site config

Redirect

Now we can create .htaccess file for redirecting our page by various HTTP code, let's create a file by the command below:

nano /etc/test/.htaccess

For make permament redirect we need to indicate HTML—page by the command and syntax below:

RewriteEngine On
RewriteRule ^start$ /target [R=301, L]
RewriteRule ^start$ index.html
RewriteRule ^target$ index2.html

In the first line we indicate enabled status of module override, at the second we can see redirect rule with code 301 , at the third we assign index.html with start label and target accordance to index2.html.

Screenshot №6 — Htaccess

Restart our web—server  and enable override module:

sudo a2enmod rewrite && systemctl restart apache2

Screenshot №7 — Restart

Let's check implementation of our redirecting, go to the web—site:

Screenshot №8 — First page

The second page looks like this:

Screenshot №9 — Target page

We have redirected, make sure by the tools in the browser, press F12 and monitor HTTP/S query from client and server:

Screenshot №10 — Network

Alright we see in the status column 301 HTTP redirect code therefore all system and web server configuration work properly! Next, if we want to change redirecting code, then we need just a little modified previous configuration:

nano /etc/test/.htaccess

Enter that code instead of previous configuration:

Redirect 302 /start http://vdushu.space/target
RewriteRule ^start$ index.html
RewriteRule ^target$ index2.html

In that case we straight indicate for redirecting by the syntax above Redirect, then choose HTTP code for this and save file! Let's check the result:

Screenshot №11 — 302 Redirect

Conclusion

The redirects serve as indispensable tools for maintaining a seamless user experience, preserving SEO rankings, and skillfully managing shifts in a company's website structure, content, and URLs. By strategically implementing various types of redirects, businesses can foster brand credibility, enhance website user—friendliness, and achieve digital prosperity. The process of implementing redirects in Apache2 involves configuring the web server and creating .htaccess files to control redirection behavior effectively. Properly setting up redirects ensures that users are directed to the right content, contributing to a successful online presence.