![]() |
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.
π Introduction: practical examples
Imagine these situations:
- You have moved the blog from
https://www.mysite.com/blog
tohttps://blog.mysite.com
- You want to force the use of HTTPS
- You want the site to respond only on
mysite.com
and not onwww.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
Redirects are essential for:
-
Change URL structure without losing visitors or rankings
-
Redirect from
http
tohttps
-
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)
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]
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]
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
RedirectMatch 301 ^/blog/(.*)$ https://blog.mysite.com/$1
π§ Difference between Redirect and Rewrite
Function | Redirect | Rewrite | ||||
---|---|---|---|---|---|---|
Visible to browser | ✅ Yes (browser changes URL) | ❌ No (URL remains visible the same) | ||||
| Simple and direct |
| ||||
Apache module | mod_alias | mod_rewrite | ||||
Suitable for | Static 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