Free .htaccess Redirect Generator

Generate Apache .htaccess redirect rules for 301, 302, and other redirect types. Force HTTPS, WWW, and more โ€” no server knowledge needed.

Advertisement ยท 728ร—90
Redirect Configuration
Generated .htaccess Code
RewriteEngine On
Advertisement ยท 300ร—250

What Is .htaccess?

The .htaccess file (short for "hypertext access") is a distributed configuration file used by Apache web servers. It allows website owners and developers to apply server-level configuration directives on a per-directory basis without modifying the main Apache server configuration file (httpd.conf). When Apache processes a request for a file in a directory, it reads the .htaccess file in that directory (and all parent directories up to the document root) and applies any directives found there.

The .htaccess file supports a wide range of functionality: URL redirects and rewrites, password protection with HTTP Basic Authentication, custom error pages (404, 500 etc.), MIME type overrides, GZIP compression settings, browser caching headers, and access control rules that allow or deny traffic from specific IP addresses. For most shared hosting users who do not have access to the main server configuration, .htaccess is the primary tool for server-level customization.

The name begins with a period (dot), which makes it a hidden file on Unix/Linux systems. If you are uploading via FTP, you may need to enable "show hidden files" in your FTP client to see and edit it. The file has no extension โ€” it is simply named .htaccess with no suffix.

301 vs 302 Redirect โ€” Which Should You Use?

The most common redirect types are 301 (Permanent) and 302 (Temporary), and choosing correctly between them has significant SEO implications. A 301 redirect signals to search engines that the original URL has permanently moved to the new location. Search engines transfer the majority of the original page's link equity (PageRank) to the new URL and update their index to reflect the new address. Use 301 redirects when you are permanently changing a URL, migrating content, or restructuring your site.

A 302 redirect signals that the move is temporary โ€” the original URL is still the canonical address and should be retained in the search engine index. Search engines do not transfer link equity for 302 redirects. Use 302 redirects only for genuinely temporary situations: A/B testing, seasonal content swaps, or maintenance pages. Using a 302 when you mean a 301 is a very common SEO mistake that can cause the old URL to continue ranking instead of the new one.

The 307 and 308 redirect types are HTTP/1.1 alternatives that guarantee the HTTP request method is preserved โ€” a POST request to a 307/308 URL will remain a POST to the new URL, rather than being converted to a GET (which older 301/302 implementations sometimes did). For most web content redirects (pages, images, assets), 301 and 302 are the correct choices. Use 307/308 only when working with form submissions or API endpoints where method preservation matters.

How to Force HTTPS with .htaccess

Redirecting all HTTP traffic to HTTPS is one of the most important security and SEO configurations for any website. Since 2014, Google has used HTTPS as a positive ranking signal, and modern browsers display "Not Secure" warnings for HTTP sites. The standard .htaccess rule for forcing HTTPS uses mod_rewrite:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

The RewriteCond line checks whether the current request is not already using HTTPS. The RewriteRule then redirects all non-HTTPS requests to the same URL but with the https:// scheme. The [L,R=301] flags mean: stop processing rules (Last) and send a 301 Permanent Redirect. Make sure your SSL certificate is installed and working before adding this rule, otherwise you will create a redirect loop.

How to Force WWW or Remove WWW

Consistency between the www and non-www version of your domain is important for both SEO and user experience. Search engines treat https://example.com/ and https://www.example.com/ as separate URLs. If both versions are accessible without a redirect, you may accumulate duplicate content issues and split your link equity across two versions of your site.

Use the "Force WWW" preset to redirect all non-www traffic to www, or the "Remove WWW" preset for the opposite. You should also set your preferred version in Google Search Console under Settings > Change of Address, and ensure your canonical tags consistently reference the preferred version. The choice between www and non-www is largely a matter of preference โ€” www was historically preferred because it allows more flexible DNS and CDN configuration, but non-www is increasingly common for modern websites.

Common .htaccess Rules

Beyond redirects, .htaccess is used for many other server configuration tasks. Custom 404 error pages improve user experience by providing navigation options when a page is not found: ErrorDocument 404 /404.html. Directory listing protection prevents Apache from displaying a file browser when no index file exists: Options -Indexes. GZIP compression improves page load speed by compressing text-based assets before sending them to the browser. Browser caching headers tell visitors' browsers to cache static assets locally, reducing repeat visit load times.

Password protecting a directory is done with AuthType Basic, AuthName, AuthUserFile, and Require valid-user directives. IP-based access control uses Order deny,allow, Deny from all, and Allow from [IP] syntax (or the newer Require ip syntax in Apache 2.4+). Always keep a backup of your .htaccess file and test changes in a staging environment first โ€” a syntax error in .htaccess returns a 500 Internal Server Error for every request to that directory.

Frequently Asked Questions

A 301 redirect is an HTTP status code that signals a permanent redirect from one URL to another. When a browser or search engine crawler requests the original URL, the server responds with status 301 and a Location header pointing to the new URL. Search engines interpret 301 redirects as a permanent change and transfer the majority of the original page's link equity (PageRank) to the destination URL, updating their index to reflect the new address. It is the most SEO-friendly redirect type for permanent URL changes.

No. .htaccess is an Apache web server feature and only works on servers running Apache with the AllowOverride directive enabled. It does not work on Nginx, IIS (Microsoft), or Lighttpd servers. Nginx is increasingly common (especially on cloud platforms like AWS and Google Cloud), and its configuration is done differently โ€” in the main nginx.conf or virtual host configuration files. If you are on Nginx, you need to convert your .htaccess rules to equivalent Nginx location blocks and rewrite rules.

Use the "Force HTTPS" preset in this generator. The generated rule uses mod_rewrite to check if the request is not already HTTPS, and if so, redirects it to the HTTPS version with a 301 status. Place this rule at the top of your .htaccess file, before other rewrite rules. Ensure you have a valid SSL certificate installed before adding this rule โ€” if the HTTPS version of your site is not accessible, the redirect will create an error loop. Most modern hosting providers offer free SSL via Let's Encrypt.

A well-implemented 301 redirect from an old URL to a relevant new URL has minimal negative SEO impact. Google passes the vast majority of link equity through 301 redirects. However, redirect chains (multiple sequential redirects) dilute equity and slow down crawling โ€” avoid chains of more than one redirect hop. Redirecting to a completely unrelated page, or having thousands of redirect loops, can cause crawl budget issues. Always redirect to the most relevant equivalent page, not just to the homepage.

Both 301 and 308 indicate a permanent redirect, but they differ in how they handle the HTTP request method. Older browser and server implementations of 301 redirects would change the request method from POST to GET when following the redirect. The 308 status code was introduced (RFC 7238) to guarantee that the original request method โ€” whether GET, POST, PUT, or DELETE โ€” is preserved through the redirect. For standard web page redirects where the method is always GET, there is no practical difference between 301 and 308. Use 308 when redirecting API endpoints or form submissions that use POST.

Several methods work well. In your browser, open DevTools (F12), go to the Network tab, enable "Preserve log," then navigate to the old URL โ€” you will see the 301 response followed by the request to the new URL. For command line testing, use curl -I http://old-url.com/old-page to see the response headers. Online tools like redirect-checker.org or httpstatus.io let you trace full redirect chains. Google Search Console's URL Inspection tool shows how Googlebot sees the redirect and whether it has been indexed.

Related Free Tools

Need a custom tool built for your business?

Get a Free Quote