All pages are not being redirected to HTTPS when accessed using HTTP

All pages are not being redirected to HTTPS when accessed using HTTP - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, wordpress, apache, https, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


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"
</i
fModule>
<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


Solution :

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.



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"
</i
fModule>

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

Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect