1. What Is the .htaccess File in Web Hosting?
The .htaccess (Hypertext Access) file is a directory-level configuration file supported by Apache and LiteSpeed web servers. It allows webmasters to manage URL routing, security headers, compression, caching lifetimes, and 301 redirects directly at the server level before PHP execution begins.
2. Essential .htaccess Redirect Recipes
- Force HTTPS:
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
- Redirect www to non-www:
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC] RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
- Single Page 301 Redirect:
Redirect 301 /old-page /new-page
- Normalize Trailing Slashes on Single Slugs:
RewriteCond %{REQUEST_METHOD} =GET RewriteCond %{REQUEST_URI} !^/(admin|install|blog)/ [NC] RewriteRule ^([a-zA-Z0-9_-]+)/$ /$1 [R=301,L]
3. Automated Generator Tool
Avoid syntax errors and server 500 crashes by using our free Redirect Rule Generator to formulate verified Apache mod_rewrite code blocks.
SEOFree Technical Review
This guide was researched, tested, and authored by the SEOFree Editorial Team in accordance with Google Search Essentials and technical SEO best practices.