Serverspace accelerated the answering of support requests by 40%
August 23, 2023
Updated August 22, 2023

How to set up redirect via .htaccess?

Debian Web server


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

  • Adapting to Changes: As companies frequently update, reorganize, or remove web pages, redirects play a vital role in ensuring a smooth transition. By automatically steering visitors toward new or relevant pages, redirects prevent the frustration of encountering broken links or error messages.
  • Safeguarding SEO Standing: Search engines like Google consider factors such as link quality and quantity when determining a page's search result ranking. Without proper redirects during website structural changes or URL modifications, a company risks losing valuable SEO rankings. Redirects are the solution, preserving search engine visibility by indicating page movements.
  • Elevating User Experience: Delivering a positive user journey is paramount to retaining and engaging website visitors. Redirects shine in this area by seamlessly guiding users to the appropriate content. This eliminates confusion and irritation that may arise when users click links or enter URLs.
  • Optimizing Marketing Endeavors: Marketing campaigns often feature specific landing pages, which might become obsolete once campaigns conclude. Redirects prove their utility by channeling traffic from these promotional URLs to other relevant destinations, ensuring a continuous flow of user engagement.

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.


  • Root rights;
  • Several knowledge about work OS ;
  • Internet connection.


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>
<title>FIRST PAGE</title>
<h1>That starting page!</h1>
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>
<title>SECOND PAGE</title>
<h1>Congratulations, you are in target site!</h1>
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:

site config
Screenshot №5 — Site config


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:

First page
Screenshot №8 — First page

The second page looks like this:

Target page
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
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:

302 Redirect
Screenshot №11 — 302 Redirect


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.

5 out of 5
Аverage rating : 5
Rated by: 1
1101 CT Amsterdam The Netherlands, Herikerbergweg 292
+31 20 262-58-98
700 300
700 300

You might also like...

We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.