πŸ” What are Redirects and how to configure them in Apache

  



In the world of the web, redirects are an essential tool for correctly managing traffic to the pages of a website. 

They are essential for maintaining a good user experience, preserving SEO positioning and ensuring that content is always reachable, even when URLs change. 

In this article we will see what they are, what types exist, what they are used for and how to configure them in the Apache server.

πŸ”— Do you like Techelopment? Check out the site for all the details!

πŸ“Œ Introduction: practical examples

Imagine these situations:

  • You have moved the blog from https://www.mysite.com/blog to https://blog.mysite.com
  • You want to force the use of HTTPS
  • You want the site to respond only on mysite.com and not on www.mysite.com

In all these cases you need a redirect: a way to tell the browser "this page has moved".


πŸ” What are redirects?

A redirect is an instruction that tells the browser or Google that a page has moved from one URL to another.

Redirect 301 /old-page https://www.mysite.com/new-page

This line says:

"If someone searches for /old-page, send them permanently to /new-page."


πŸ”§ Types of redirects

πŸ” 301 – Permanent Redirect

  • When to use it: If a page has been permanently replaced

  • SEO: The value is almost entirely transferred

  • Example:

Redirect 301 /contacts https://www.mysite.com/contact-us

πŸ” 302 – Temporary redirect

  • When to use it: whether the old page will be available again in the future

  • SEO: does not transfer authority

  • Example:

Redirect 302 /event https://www.mysite.com/event-2025

πŸ” 307 – Temporary redirect (HTTP/1.1)

  • Similar to 302, but ensures that the HTTP method (GET, POST) remains the same. Useful for forms and submitted data.

πŸ” Meta Refresh (HTML)

  • It is used in an HTML page, it is not server-side

  • Less professional and not recommended for SEO.

<meta http-equiv="refresh" content="5;url=https://www.mysite.com/new-page">


Why use redirects?

Redirects are essential for:

  • Change URL structure without losing visitors or rankings

  • Redirect from http to https

  • Unify URLs (e.g. from www to non-www)

  • Avoid 404 errors

  • Migrate domains or CMS

  • Temporarily send to a page maintenance


πŸ“ How to configure redirects in Apache

✅ Method 1: .htaccess file with Redirect 

Ideal if your hosting supports it (typical in Apache servers).

πŸ”Ή Permanent redirect of a page:

Redirect 301 /about-us https://www.mysite.com/company

Meaning: every visit to /about-us will be permanently redirected to /company.


✅ Method 1: file .htaccess with Rewrite (conditional)

More powerful, allows you to write conditional rules. Let's see a complete example:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Line by line explanation:

  • RewriteEngine On
    Enable the rewrite engine (mod_rewrite)

  • RewriteCond %{HTTPS} off
    This line enforces a condition: the following rule applies only if the site is in HTTP (not secure)

  • RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    Redirects any requested URL to the HTTPS version of the same address

    • ^(.*)$ → It is a regular expression (regex) that Apache uses to recognize URLs to redirect. In this case it indicates any URL requested, from start to end. Apache captures it in an implicit variable called $1 ( although in this case it is not used because the rewrite is direct)

    • %{HTTP_HOST} → the requested domain (e.g. www.mysite.com)

    • %{REQUEST_URI} → the path to the page (e.g. /products)

    • R=301 → permanent redirect

    • L → stops reading other rules

Practical example:

A request to:

http://www.mysite.com/products

is redirected to:

https://www.mysite.com/products

✅ Method 2: Configuration in the file httpd.conf

For those who have access to the server (e.g. VPS or dedicated):
<VirtualHost *:80>
ServerName www.mysite.com 
Permanent redirect / https://mysite.com/
</VirtualHost>

Meaning: anyone who accesses any URL with www.mysite.com is sent to the main site in HTTPS.


πŸ”„ More useful examples with Rewrite

πŸ”„ From www to non-www:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^(.*)$ https://mysite.com/$1 [L,R=301]

πŸ”„ From non-www to www:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ https://www.mysite.com/$1 [L,R=301]

πŸ”„ Redirect an entire directory:

RedirectMatch 301 ^/blog/(.*)$ https://blog.mysite.com/$1


🚧 Difference between Redirect and Rewrite

FunctionRedirectRewrite
Visible to browser✅ Yes (browser changes URL)❌ No (URL remains visible the same)
Instruction type
Simple and direct
More powerful and flexible
Apache modulemod_aliasmod_rewrite
Suitable forStatic URLs and Clear redirects (e.g. 301)Dynamic conditions (e.g. HTTPS)

✅ Conclusion

Redirects are essential for:

✅ Avoid 404 errors
✅ Do not lose SEO positioning
✅ Maintain a good user experience
✅ Make orderly transitions between pages or domains

Apache provides us with two main tools:

  • Redirect → simple and direct

  • Rewrite → powerful and customizable

Both can be written in .htaccess or in the server configuration file (httpd.conf).



Follow me #techelopment

Official site: www.techelopment.it
facebook: Techelopment
instagram: @techelopment
href="https://t.me/techelopment_channel">@techelopment_channel


whatsapp: Techelopment
youtube: @techelopment