Mark Barnes
Mark Barnes

Reputation: 468

Why are rules after my .htaccess <If></If> ignored?

I'm running a WordPress site, and I want to redirect http to https for some domains, so I have these .htaccess rules (Apache 2.4):

<If "%{HTTP_HOST} in { 'secure.mydomain.com', 'ssl.mydomain.com' }">
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</If>

# BEGIN WordPress
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

This works fine for non-https domains (e.g. www.mydomain.com). But the WordPress rules are not being applied to https://secure.mydomain.com or https://ssl.mydomain.com. When I use those domains, I get 404 errors. I don't get the 404 errors if I remove the IF statement. It's almost as if apache thinks the WordPress rules are wrapped in an else statement.

So, a simple question. What is wrong with these rules?

Upvotes: 0

Views: 42

Answers (1)

Mark Barnes
Mark Barnes

Reputation: 468

CBroe's comment got me off onto the right path. According to this blog, RewriteCond is not supported inside an <If> block.

I need to rewrite the rule so either it only uses RewriteCond, or it only uses <If>. Like this:

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^secure\.mydomain\.com [OR]
RewriteCond %{HTTP_HOST} ^ssl\.mydomain\.com
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

or this:

<If "%{HTTP_HOST} in { 'secure.mydomain.com', 'ssl.mydomain.com' } && %{HTTPS} == 'off'">
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</If>

Upvotes: 3

Related Questions