Home > Net >  Why doesn't my .htaccess file redirect to https? How to fix?
Why doesn't my .htaccess file redirect to https? How to fix?

Time:09-25

Oh Jesus, I've tried this a million times, a million different ways using Stackoverflow, using blogs, my web host even did it at one point, to no success, or maybe my fiddling with caching broke it.

I want to redirect all HTTP:// traffic to https, it's detering folk from buying.

So this is the content of the file, what needs to change???

AddType text/html .shtml
AddHandler server-parsed .shtml
Options Indexes FollowSymLinks Includes
AddHandler server-parsed .html .htm
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml xml
AddOutputFilterByType DEFLATE application/rss xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilter DEFLATE .shtml
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.pottertour.co.uk/$1 [R,L]
</IfModule>

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 10 days"
    ExpiresByType text/html "access plus 1 day"
    ExpiresByType text/css "access plus 0 day"
    ExpiresByType text/plain "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/webp "access plus 1 month"  
    ExpiresByType application/x-javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 week"
    ExpiresByType application/x-icon "access plus 1 year"
</IfModule>
# END EXPIRES

OK I tried removing the <IfModule mod_rewrite.c> wrappers as per MrWhite's comment, and it seemed to put the loading in a constant loop. So I returned it to what it was previously, but without the wrappers. As per below.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^pottertour.co.uk [NC]   
RewriteRule ^(.*)$ https://www.pottertour.co.uk/$1 [L,R=301]   
  • It's now blocking the loading of my google fonts and styles.css. Really CRITICAL!
  • It also fails to redirect.

Solutions and explanations well appreciated.

OK well thanks to MrWhite I've had another go, the whole .htacess file now stands at this:

AddType text/html .shtml
AddHandler server-parsed .shtml
Options Indexes FollowSymLinks Includes
AddHandler server-parsed .html .htm
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml xml
AddOutputFilterByType DEFLATE application/rss xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilter DEFLATE .shtml
</IfModule> 

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 10 days"
    ExpiresByType text/html "access plus 1 day"
    ExpiresByType text/css "access plus 10 day"
    ExpiresByType text/plain "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/webp "access plus 1 month"  
    ExpiresByType application/x-javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 week"
    ExpiresByType application/x-icon "access plus 1 year"
</IfModule>
# END EXPIRES

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

Note I've removed all references to a permanent redirect by omitting the R=301

I've set a canonical https:// URL on all pages anyway, so I think adding to .htaccess doesn't help much.

It's still not accessing my styles.css or downloading google fonts. Nor is the browser executing on page javascript accordions correctly (how bizarre it's embedded on the page!)

I've contacted 1&1 my web host and asked them to ensure the .htacess file is 'refreshed'... I'll go off and research Cloudfare.

CodePudding user response:

Your edits/comments don't really help unfortunately. However, having had a quick look at your site, you would seem to be using Cloudflare (which acts a proxy in front of your application server), in which case you would need to do something like the following instead to redirect HTTP to HTTPS:

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

You do not need the <IfModule> wrapper.

You should first test with a 302 (temporary) redirect to avoid caching issues and only change to a 301 (permanent) redirect once you have confirmed it works as intended. 301s are cached persistently by the browser so can make testing problematic.

You need to clear your browser cache before testing.

This does not canonicalise the www/non-www subdomain.

  • Related