1

I'm running a Wordpress blog that has an issue with HTTPS redirect. Except home page, no other URL is being redirected to HTTPS if you visit via HTTP.

I want to redirect all HTTP traffic to HTTPS. Currently, only the Home page(http://www.example.com) is being redirected to HTTPS if you try with HTTP.

but if you visit http://www.example.com/page1, then it won't be redirected to HTTPS and stays at HTTP.

I don't want to use any plugin such as "really simple SSL". After a lil'bit of searching over net, I found that I can modify the .htaccess file to do that. Then I tried to understand .htaccess file (considering I never worked with PHP or WordPress or even Apache before). I got to know that RewriteEngine On should appear only once in your file but in my case, it's appearing twice. maybe some plugin or theme had modified this that i'm not aware of. below is the content of my .htaccess file.

# 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_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 24 hours"
    ExpiresByType image/jpeg "access plus 24 hours"
    ExpiresByType image/gif "access plus 24 hours"
    ExpiresByType image/png "access plus 24 hours"
    ExpiresByType text/css "access plus 24 hours"
    ExpiresByType application/pdf "access plus 1 week"
    ExpiresByType text/javascript "access plus 24 hours"
    ExpiresByType text/html "access plus 5 minutes"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresDefault "access plus 24 hours"
</IfModule>
<ifModule mod_headers.c>
Header set X-Endurance-Cache-Level "2"
</ifModule>
<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
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I don't have any subdomains. only a single domain. So to clear my doubts, I have two questions.

1) Is this .htaccess file correct? I mean can we have two RewriteEngine On lines? that too outside the , again I'm not very familiar with this syntax.

2) What should I change to reflect HTTPS redirection on all of my pages?

Moreover, it also broke the Non-WWW to WWW redirection.

PS: these are one of the links that I have visited-

https://wordpress.org/support/article/htaccess/

.htaccess syntax multiple RewriteEngine on

https://www.hostinger.com/tutorials/ssl/forcing-https

0

Your HTTPS rewrite rule never gets executed because it is after RewriteRule . /index.php [L]. That rule matches all URL paths and is the last rule executed for them because of the L flag.

To make that redirect rule work, simply move it to the top of the .htaccess file.

WordPress itself does some redirects. What is likely happening here is that the one redirect you see is done by the WordPress PHP code rather than done by .htaccess.

| improve this answer | |
  • So you mean, i just need to move last 3 lines at the top of file (i.e before <IfModule mod_expires.c>) ? could that, possibly, cause any issue? – user3810188 May 23 at 16:53
  • I'd put them before the # BEGIN WordPress. WordPress can overwrite everything in its section. It won't cause any problems to move those rules up, it will make them start working. – Stephen Ostermiller May 23 at 17:48
  • Thanks, I can't upvote the answer as I have less than 15 reputations. just marked it as answer. – user3810188 May 23 at 17:51
  • "the one redirect you see is done by the WordPress PHP code" - The preceding WP front-controller doesn't catch requests for the home page (it doesn't match the pattern . and it is also a directory, so the condition would also fail), so it's quite likely that the redirect in .htaccess - at the end of the file - is triggering for this request (as it would do for any static resource as well). "That rule matches all URL paths" - all URL-paths that are at least 1 character long (excluding the slash prefix). – MrWhite May 23 at 21:11
0

By taking inspiration from @Stephen's answer, I solved it by moving the last 3 lines just before the mod_rewrite block.

# 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_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 24 hours"
    ExpiresByType image/jpeg "access plus 24 hours"
    ExpiresByType image/gif "access plus 24 hours"
    ExpiresByType image/png "access plus 24 hours"
    ExpiresByType text/css "access plus 24 hours"
    ExpiresByType application/pdf "access plus 1 week"
    ExpiresByType text/javascript "access plus 24 hours"
    ExpiresByType text/html "access plus 5 minutes"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresDefault "access plus 24 hours"
</IfModule>
<ifModule mod_headers.c>
Header set X-Endurance-Cache-Level "2"
</ifModule>

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

<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
| improve this answer | |
  • WordPress can overwrite anything between BEGIN WordPress and END WordPress, so I would move it further up. If you were to put it in there, I would put inside the top of the IfModule mod_rewrite.c block and remove the redundant RewriteEngine On. – Stephen Ostermiller May 23 at 17:55
  • Can you update your answer with the updated script? it might be confusing for beginners – user3810188 May 23 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.