Apache redirects with .htaccess: HTTPS, www and 301s
Write Apache redirects that work the first time. Force HTTPS, pick www or non-www, move single pages and whole domains with Redirect and RewriteRule, and avoid redirect loops.
- Beginner
- Linux VPS
- 5 min read
Redirects keep visitors and search rankings when URLs change. Apache gives you two tools: the simple Redirect directives from mod_alias, and RewriteRule from mod_rewrite for anything that depends on the host name, the protocol or a pattern. This guide gives tested examples for the common jobs and explains how to avoid the loops that break sites.
Before you start
- Apache with
mod_rewriteenabled and.htaccessallowed for your site. If rules are ignored, follow Enable .htaccess and mod_rewrite in Apache first. - For HTTPS redirects, a working certificate. See Secure Apache with Let's Encrypt.
- A backup of your current
.htaccess:
cp /var/www/example.com/public/.htaccess ~/htaccess.bak301 or 302?
- 301 (permanent): the page has moved for good. Browsers and search engines remember it and pass ranking to the new URL.
- 302 (temporary): the move is short-term. Nothing is cached for long.
Test with 302 first, then switch to 301 once the redirect is right. Browsers cache 301s hard, so a wrong 301 keeps hurting you after you fix it. Use a private window or curl while testing.
Redirect single pages with Redirect
For a one-to-one move inside the same site, Redirect is the simplest option and needs no rewrite engine:
Redirect 301 /old-page /new-page
Redirect 301 /blog/2019-post https://example.com/blog/updated-postTo move a whole folder:
Redirect 301 /docs /helpThat sends /docs/install to /help/install, because Redirect matches the start of the path and keeps the rest.
For patterns, use RedirectMatch with a regular expression:
RedirectMatch 301 ^/category/(.*)$ /topics/$1Do not mix Redirect lines and RewriteRule lines that touch the same URLs. The two modules run at different stages, so the order in the file does not decide which wins. Pick one style per job.
Force HTTPS
Put this at the top of .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]The condition matches only plain HTTP requests, so the rule cannot loop on HTTPS.
If you control the virtual host, an even cleaner way is to make the port 80 vhost do nothing but redirect:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://example.com/
</VirtualHost>Choose www or non-www
Pick one version and send the other to it. To force non-www:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]To force www:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]Both rules send the visitor straight to HTTPS, so you avoid a chain of two redirects. Place them above the HTTPS rule, or combine them as below.
HTTPS and non-www in one hop
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]Move an old domain to a new one
Point the old domain's DNS at your server and add it as a ServerAlias (or give it its own vhost). Then in its .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]Every path on the old domain maps to the same path on the new one. Keep the old domain registered and redirecting for at least several months so links and search engines catch up.
Avoid redirect loops
A loop happens when the target of a redirect matches the rule again. Common causes:
- A proxy or CDN in front, such as Cloudflare in Flexible mode. Apache sees plain HTTP on every request, so
%{HTTPS} offis always true. Set the CDN to Full (strict) mode, or check the forwarded header instead:
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]- The app redirects too. WordPress redirects to the address in its settings. If that says
wwwand Apache forces non-www, they fight. Make them agree. - A rule with no condition that redirects every URL, including the one it redirects to.
Check it works
Use curl to see each hop without browser caching:
curl -I http://www.example.com/old-pageLook for HTTP/1.1 301 Moved Permanently and the Location: header. To follow the whole chain:
curl -sIL http://www.example.com/old-page | grep -iE "^(HTTP|location)"Ideally you see one 301 and then a 200. More than one redirect before the 200 means you can merge rules.
Troubleshooting
- "Too many redirects" in the browser: a loop. Check the proxy case above, then compare your rules with the site URL set in your app.
- Redirect works in curl but not in your browser: your browser cached an old 301. Test in a private window or clear the site's cache.
- Rules do nothing:
.htaccessis ignored orRewriteEngine Onis missing. See the.htaccessguide linked above. - 500 error after editing: a typo in a rule. Restore your backup and read the error log, usually
/var/log/apache2/error.logor/var/log/httpd/error_log. - Query strings disappear or double up:
%{REQUEST_URI}does not include the query string, but Apache appends it to the target automatically unless you add theQSDflag.
Next steps
- Lock down the server next: Harden Apache security headers.
- Speed up the site: Enable gzip compression in Apache.
- Hosting several sites? Our Linux VPS plans give you full root access.
Was this guide helpful?
Still stuck?
Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.
Related guides
Websites & apps
Install PHP 8.x on AlmaLinux and Rocky Linux with Remi
Enable EPEL and the Remi repository on AlmaLinux or Rocky Linux 8 and 9, choose a PHP module stream, install PHP-FPM with common extensions, and run extra PHP versions side by side.
- Intermediate
- Linux VPS
- 4 min read
Websites & apps
Use Apache as a reverse proxy with mod_proxy
Put Apache in front of a Node.js, Python or Docker app with mod_proxy. Set ProxyPass and ProxyPassReverse, keep the original host, proxy WebSockets and serve the app over HTTPS.
- Intermediate
- Linux VPS
- 4 min read
Websites & apps
Enable gzip compression in Apache with mod_deflate
Compress HTML, CSS, JavaScript and JSON in Apache with mod_deflate, add Brotli with mod_brotli for modern browsers and check the Content-Encoding header to prove it works.
- Beginner
- Linux VPS
- 4 min read