Apache2 conditional section based on port

Solution 1:

Yes, starting from Apache 2.4, you can use

<If "%{HTTPS} == 'on'">
    # HTTPS-specific configuration here
</If>

in your configuration file.

Many other expressions inside the <If "expression"> bracket are also supported, e.g. actually checking for a specific port, as you suggested. For more details see the Apache docs.

However, keep in mind that some directives will not work within an <If> clause. Check for the allowed contexts of a directive, where <If> counts as a directory context (like <Directory>).