Score:0

Apache RewriteCond/RewriteRule with variables causes infinite redirects

in flag

I'm trying to use variables in the RewriteCond and RewriteRule like so:

SetEnv DOMAIN "example.com"
SetEnv SUBDOMAINS "www|assets"

#more config stuff, unrelated

#I separated the HTTPS redirect just in case, it didn't change anything.
RewriteCond %{HTTPS} !on [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} !^(%{ENV:SUBDOMAINS})\.%{ENV:DOMAIN}$ [NC]
RewriteRule ^(.*)$ https://www.%{ENV:DOMAIN}/$1 [L,R=301]

This causes a redirect loop. This is an optimization of what's working right now which is explicitly defining

RewriteCond %{HTTP_HOST} !^(www|assets).example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

I'm trying to have a htaccess that will work across multiple websites with minimal changes, so only the variables are changed from website to website.

What could be causing such a problem?

ezra-s avatar
ru flag
you should show an example of the request and response with "curl -I"or similar. About checking https...why not just set a not SSL virtualhost and just plain redirect to https from there? Convolutions and these continues redirects with greedy matches just tend to cause a mess.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.