Reputation: 3392
I have the code below in my .htaccess to redirect all pages to https except one (/wc-api/v2), which must NOT use SSL.
It does redirect all pages to https successfully, but if I go to /wc-api/v2, it redirects me to /index.php.
# Custom Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !^on$
RewriteCond %{REQUEST_URI} !^/wc-api/v2
RewriteRule (.*) https://example.com/$1 [R,L]
</IfModule>
# End Custom Redirects
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
FYI The second block of redirects is required for Wordpress. I also cannot combine them into one single block or Wordpress will overwrite my changes.
Upvotes: 2
Views: 392
Reputation: 1
I got an error on my server with the suggestion above so modified it slightly and found that this worked for me:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{REQUEST_URI} !^/wc-api/v2
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule (.*) https://www.example.com/$1 [R,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Hope it works for you.
Upvotes: 0
Reputation: 96281
When rewriting is configured in .htaccess
context, after your last rule is applied, the whole process starts over again, using the internally rewritten URL as the new “input”, until no more rules match.
That means, when you request /wc-api/v2
, it is not rewritten to HTTPS, because it fails your first RewriteCond
, and so the rule in the next block rewrites it to /index.php
internally.
Now the “next round” starts, with /index.php
as input. RewriteCond %{REQUEST_URI} !^/wc-api/v2
now does result in “true”, because the REQUEST_URI is not /wc-api/v2
any more, it is now /index.php
. Therefor, the RewriteRule
is applied – and you are redirected to https://example.com/index.php
To avoid this, you must add another RewriteCond
, which will prevent the rule from being applied for the REQUEST_URI /index.php
as well:
RewriteCond %{HTTPS} !^on$
RewriteCond %{REQUEST_URI} !^/wc-api/v2
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule (.*) https://example.com/$1 [R,L]
Upvotes: 4