If you’re running a website on an Apache server, the .htaccess file is one of the most powerful tools at your disposal. It allows you to control how your URLs behave — from redirections to clean URL rewriting — without needing to change your actual server configuration.
In this article, we’ll cover the basics of URL redirection and rewriting using .htaccess, with clear examples and explanations.
.htaccess stands for Hypertext Access. It’s a configuration file used on Apache-based web servers that allows you to make directory-level changes to server behavior.
You can:
Redirect traffic from one URL to another
Create clean URLs (e.g., /about instead of /about.php)
Restrict access to certain pages
Enable HTTPS
And much more
Use this when a page has permanently moved to a new URL.
Use this when the redirection is temporary.
You can also redirect using RewriteRule:
URL rewriting is used to make URLs more user- and SEO-friendly. For example, instead of:
You can use:
This tells Apache:
When someone accesses /product/123
Internally rewrite it to product.php?id=123
This allows users to access example.com/about instead of example.com/about.php.
Always backup your .htaccess file before making changes.
Use [L] to stop further rules from being processed once a match is found.
Test your redirects with tools like httpstatus.io.
Don’t mix Redirect and RewriteRule too much in the same block — prefer RewriteRule for consistency.
The .htaccess file is a powerful configuration tool that gives you granular control over your Apache-based website — often without touching your core server files. Whether you want to redirect outdated pages, enforce HTTPS, improve SEO through clean URLs, or restrict access to sensitive areas, .htaccess makes all of this possible with just a few lines of code.
Understanding and mastering even a handful of .htaccess directives can dramatically improve your website’s performance, security, and usability. It allows you to respond quickly to changes in site structure, prevent broken links, and maintain a consistent experience for your visitors.
Imagine you’ve restructured your blog and want to redirect traffic from an outdated post to a new location:
This simple line ensures that:
Search engines transfer SEO value to the new URL
Users don’t land on a 404 page
Redirects happen instantly with a 301 (permanent) status code