Cookies ahead

Our support chat tool "Intercom" would like to collect some more data on you. See the related link for more details.

Docs

Redirects with .htaccess

Reviewedbyfl

Markdown ↓

Route traffic with smart redirects.

The most common use case for `.htaccess` is to re-write URLs with `mod_rewrite`. You can direct requests to a subdirectory, or specific domain, prettify URLs by omitting file endings, force https and much more.

Redirect all requests to https

Use .htaccess with mod_rewrite to redirect all http:// requests to https://, enforcing secure connections for your application. This improves security and is recommended for all production apps.

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,N]
raw

Order matters. This rule should be one of the first rules, placed at the top of your .htaccess file. Please note the X-Forwarded-Proto header: other code snippets may not work here because Apache runs behind load-balancers that handle HTTPS encryption, not Apache itself. Many CMS and frameworks offer convenient settings for this.

The R=301 flag sends a permanent 301 redirect, which browsers cache hard. Use R=302 for a temporary 302 redirect while a rule is still in flux.

Redirect all requests to the primary domain

When using a custom domain, set up domain-based redirects in .htaccess to prevent duplicate content and search engine issues by directing all traffic to your primary domain. This consolidates your site's authority and improves SEO.

# From test domain to your domain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*\.frbit\.app$ [NC]
RewriteRule .* https://www.your-domain.example%{REQUEST_URI} [R=301,L,N]
raw

A CMS will have its own options for that. If you use a redirect, always set a canonical URL pointing to your primary custom domain so that search engines know there's only one source of the content.

Written by a human. Review, grammar checks and typo fixes by AI.

AI use & editorial processEdit on GitHub ↗