Following another question I just solved, I wonder: is there anything we can do within Apache to avoid repeating lines of configuration? What about outside apache? In my case it would be AWS EC2's RHEL4, but I trust any **nix* would have a similar solution. Maybe something like sed
, maybe using .htaccess
... Dunno. But this should be pre-processed, nothing at runtime or something: just like the conf files, once apache is loaded that is it.
Here I'll copy my past solution just to illustrate:
UseCanonicalNames off
NameVirtualHost *:8888
<VirtualHost *:8888>
ServerName example1.com
ServerAlias *.example1.com
# below, stuff that will be repeated
# redirect to HTTPS
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^login\.(.*)$
RewriteRule ^(.*) https://%1/login$1 [L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.com)$
RewriteRule ^/login(.*) https://%1/login$1 [L]
</VirtualHost>
<VirtualHost *:8888>
ServerAlias *
VirtualDocumentRoot /var/www/html/%-3
# below, stuff that need to be repeated
# redirect to HTTPS
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^login\.(.*)$
RewriteRule ^(.*) https://%1/login$1 [L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.com)$
RewriteRule ^/login(.*) https://%1/login$1 [L]
</VirtualHost>
And I'm sure there are many instances in which things like this redirection_to_HTTPS
could have been a function / procedure / macro / template / include / etc.