apache HTTPX-Forwarded-Proto in .htaccess is causing redirect loop in dev environment
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Redirect loops with X-Forwarded-Proto usually happen when your app or rewrite rules disagree about whether the original request was HTTP or HTTPS. This is common in development stacks where proxy behavior differs from production. The fix is to trust forwarded headers only in the right environment and define one canonical redirect rule.
Why the Loop Happens
A typical loop sequence is:
- Client requests HTTP.
- Proxy forwards to Apache and sets or omits
X-Forwarded-Proto. .htaccessforces HTTPS based on current condition.- Upstream rewrites again because it still thinks request is HTTP.
- Browser repeats until too many redirects.
In dev, this often occurs when local reverse proxy does not set headers exactly like production load balancer.
Safe Rewrite Pattern in .htaccess
Use a single redirect condition that checks both direct HTTPS and forwarded protocol.
This prevents double-redirect logic when proxied requests are already HTTPS externally.
Ensure Proxy Sends Correct Header
If Apache is behind Nginx, set forwarded headers explicitly.
For Apache reverse proxy fronting another backend, pass equivalent headers in vhost config.
Consistency across all proxy layers is critical.
Development Environment Strategy
In local development, you may not need forced HTTPS at all. You can scope redirect rules to production hostnames.
This avoids loops on local hostnames where TLS is not configured.
Debugging Checklist
Validate each step before changing multiple rules at once.
Inspect response status and Location headers. If both requests redirect, condition logic is too broad. If neither redirects in production, proxy headers may be missing.
Enable rewrite logging at server level during troubleshooting to see which rule matched.
Protect Against Header Spoofing
Do not trust X-Forwarded-Proto from arbitrary direct internet clients. Only trust it when traffic comes from your known proxy network.
At proxy layer, strip incoming forwarded headers and set your own trusted value.
At application layer, configure trusted proxies so framework URL generation uses the expected scheme. Otherwise, apps may emit HTTP links and trigger repeated redirects even when Apache rules are correct.
Prefer Server Config Over .htaccess for Complex Rules
When possible, move rewrite logic from .htaccess to virtual host config. Server-level config is faster, easier to audit, and less likely to conflict with nested directory rules.
A clean setup is:
- Proxy or load balancer terminates TLS.
- One canonical HTTPS redirect rule at edge or vhost layer.
- Application trusts forwarded headers only from known proxy hops.
This architecture prevents most redirect loops before they reach application code.
Common Pitfalls
- Forcing HTTPS both at proxy layer and
.htaccesswithout shared conditions. - Trusting
X-Forwarded-Protofrom untrusted direct client traffic. - Applying production redirect rules unchanged in local environment.
- Using multiple overlapping rewrite files in nested directories.
- Forgetting to clear browser redirect cache after rule updates.
Summary
- Redirect loops come from inconsistent protocol detection across proxy and Apache layers.
- Use one canonical rewrite condition that checks both
HTTPSandX-Forwarded-Proto. - Ensure proxies set forwarded protocol headers consistently.
- Scope HTTPS-forcing rules to appropriate environments when needed.
- Debug with
curland response headers before broad rewrite changes.
Related reading
- Apache Kafka 1.0.0 Streams API Multiple Multilevel groupby
- Apache Kafka client with selector?
- Apache kafka consumer java.security.cert.CertificateException No subject alternative names present
- Apache Kafka How to find out consumer group of a topic?
- API Gateway - POST multipart/form-data
- API Gateway CORS no 'Access-Control-Allow-Origin' header
- Api gateway get output results from step function?
- API Gateway throttling -- burst limit vs rate limit

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.