Code for Forcing https://

Jason76

Madly Diligent
Joined
Nov 27, 2016
Messages
7,211
Reaction score
960
FP$
576
Well, you still have to buy the SSL first, though. You know, sometimes it doesn't resolve to the https:// Anyone had that problem?

Put code into .htacces Normally, this is all I put in. There's nothing along with it..

Code:
#    Mod_security can interfere with uploading of content such as attachments. If you
#    cannot attach files, remove the "#" from the lines below.
#<IfModule mod_security.c>
#    SecFilterEngine Off
#    SecFilterScanPOST Off
#</IfModule>

ErrorDocument 401 default
ErrorDocument 403 default
ErrorDocument 404 default
ErrorDocument 405 default
ErrorDocument 406 default
ErrorDocument 500 default
ErrorDocument 501 default
ErrorDocument 503 default

<IfModule mod_rewrite.c>
    RewriteEngine On
  
 
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?funbizforum\.com
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/.+$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://funbizforum.com/$1 [R,L]

    #    If you are having problems with the rewrite rules, remove the "#" from the
    #    line that begins "RewriteBase" below. You will also have to change the path
    #    of the rewrite to reflect the path to your XenForo installation.
    #RewriteBase /xenforo

    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
    RewriteRule ^.*$ index.php [NC,L]
</IfModule>
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php72” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php72 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
 
You can put on top

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

or

With www

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Without www

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

also check .htaccess file in root directory
 
You can also use Content Security Policy to coerce any URLs you click on a page to HTTPS, although this might break the link, if one of the sites someone links to doesn't support HTTPS (something like 80% of sites use HTTPS, if I recall, with the number rapidly growing).

I don't have the Apache syntax in my head right now, but it would be something like:

Content-Security-Policy: upgrade-insecure-requests

This likely won't work for the initial page load, but for anything else it should.

I usually have a combination of that and a simple 301 redirect handler in Go.
For example:

Code:
// HTTPSRedirect is a connection handler which redirects all HTTP requests to HTTPS
type HTTPSRedirect struct {
}

func (red *HTTPSRedirect) ServeHTTP(w http.ResponseWriter, req *http.Request) {
    w.Header().Set("Connection", "close")
    counters.RouteViewCounter.Bump(141)
    dest := "https://" + req.Host + req.URL.String()
    http.Redirect(w, req, dest, http.StatusTemporaryRedirect)
}

Code:
go func() {
    log.Print("Listening on port 80")
    c.StoppedServer(newServer(":80", &HTTPSRedirect{}).ListenAndServe())
}()
 
Last edited:
Back
Top Bottom