# Redirects with .htaccess

Source: https://docs.fortrabbit.com/dev/htaccess/redirects
Updated: 2026-07-07
Reviewed: 2026-07-07

> Rewrite and redirect URLs with mod_rewrite in .htaccess: force HTTPS, forward a domain, drop file endings and route into a subdirectory.


<!-- TODO: Review. Old Stuff -->

## 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.



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

**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](/dev/http-status-codes/others/301), which browsers cache hard. Use `R=302` for a temporary [302 redirect](/dev/http-status-codes/others/302) while a rule is still in flux.

## Redirect all requests to the primary domain

When using a [custom domain](/platform/objects/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.



```raw
# 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]
```

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.

---

- [Domain](/platform/objects/domain)
- [Domain forwarding service](/platform/dns/domain-forwarding-service)
- [ANAME / ALIAS records](/integrations/dns/aname-alias-records)
- [301 redirect](/dev/http-status-codes/others/301)
- [302 redirect](/dev/http-status-codes/others/302)
