.htaccess Redirect Generator
Generate Apache .htaccess redirect rules including 301/302 redirects, HTTPS and www enforcement.
Frequently Asked Questions
What is an .htaccess file?
An .htaccess file is a per-directory configuration file used by the Apache web server. It allows you to set configuration directives for the directory it is placed in (and its subdirectories) without modifying the main server configuration. Common uses include URL redirects and rewrites (via mod_rewrite), custom error pages, access control, HTTPS enforcement, and setting HTTP headers. It is supported on most shared hosting environments running Apache.
What is the difference between a 301 and 302 redirect in .htaccess?
A 301 (Moved Permanently) redirect tells browsers and search engines that the URL has permanently moved. Search engines transfer link equity to the new URL and update their index. A 302 (Found) redirect indicates a temporary move — search engines keep indexing the original URL and do not transfer link equity. Use 301 for permanent URL changes (domain migrations, restructuring) and 302 only for genuinely temporary situations like A/B testing or maintenance pages.
How do I force HTTPS using .htaccess?
Add these lines to your .htaccess: RewriteEngine On, RewriteCond %{HTTPS} off, RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]. This redirects all HTTP traffic to HTTPS. Ensure your SSL certificate is valid before enabling this redirect — otherwise you may lock yourself out of the site. After enabling, check that the redirect is working correctly before adding the HSTS header to extend this protection.
What is mod_rewrite and is it required for redirects?
mod_rewrite is an Apache module that enables powerful URL rewriting and redirection using regular expressions. Most redirect rules in .htaccess use mod_rewrite (via RewriteEngine On and RewriteRule). It must be enabled on the server — most shared hosting providers enable it by default. A simpler alternative for basic redirects is the Redirect directive (e.g. Redirect 301 /old-page /new-page), which works without mod_rewrite and is sufficient for static URL redirects.