skip to Main Content

I encountered this strange issue that whenever I add this most generally used htaccess code, I get too many redirections.

Scenario:

+public_html
|
|--- WordPress 1
|--- .htaccess
 --+ /blog
   |
   |--WordPress 2
    --.htaccess

.htaccess (WordPress 2)

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.test/blog/$1 [R=301,L]

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#RewriteCond %{HTTPS} off
#RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /blog/
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
  
</IfModule>

# END WordPress

Whenever I add HTTPS redirection code in any of the htaccess it gives Too many redirects.

It seems RewriteCond %{HTTPS} off this condition is always getting true and that’s why (Checked here: http://htaccess.madewithlove.be/)
How can I tackle this?

Notes: WordPress home/site URL is set correctly: https://domain.test/blog for WordPress 2

Edit:
Solution: The issue was Flexible mode in Cloudflare. When I set it to Full SSL mode in Cloudflare the issue was solved.

2

Answers


  1. Chosen as BEST ANSWER

    The issue was Flexible mode in Cloudflare. When I set it to Full SSL mode in Cloudflare the issue was solved.


  2. In your file ‘.htaccess WordPress 2 you have a mistake.

    RewriteRule ^(.*)$ https://www.domain.test/blog/$1 [R=301,L]
    

    This is causing you problems. What happens:

    1. Say, your input is http://www.domain.test/blog
    2. The line RewriteCond %{HTTPS} off will trigger, since you are using http and not https
    3. Meaning the newxt rewrite rule gets executed.

    This part: ^(.*)$ means:

    1. take everything from beginning (^) to end ($). (In your case that is blog.)

    2. Put that part into a variable. That is the parentheses part (.*) means. It will make the ‘everything’ part available as variable $1

    3. Putting this together, you should now replace $1 with blog. The end result being:

      https://www.domain.test/blog/blog/

    So, you should start by removing the blog part of your third line:

    RewriteRule ^(.*)$ https://www.domain.test/$1 [R=301,L]
    

    New problems might occur, but you have to fix this first.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search