I can't get my .htaccess file to work to force HTTPS traffic
Site structure that I see if I ftp to my hosting provider:
/
domains
mydomainname.com
public_html
blog
- The files for my site start in
public_html
(index.html
etc) - I have a WordPress installation in
blog
- WordPress settings have been set to https: (General settings mention
https://www.mydomainname.com/blog
as the WordPress URL). - SSL certificate is working fine if I go to the domain
- Initially there was only a
.htaccess
inblog
, containing:
# BEGIN WordPress
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]# END WordPress
All kinds of tutorials I see mention that I should add this to the start of .htaccess
:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mydomainname.com/$1 [R,L]
.. and that I should place this file 'in the root of my site'.
If I have no (other) .htaccess
file:
- I can browse go to
https://www.mydomainname.com
, follow links into the WP blog and browse around there, all https: - If I go to
http://www.mydomainname.com
, follow links into the WP blog, these will turn into https:
If I place a modified htaccess in several locations I get issues like (depending on what .htaccess
contents/locations I'm trying):
- If I go to
www.mydomainname.com
it redirects tohttps://www.mydomainname.com
and I get "Server not found"; or: - No forcing from http: to https:
Questions:
- Should the
.htaccess
be placed inmydomainname.com
orpublic_html
(i.e. which folder is that famous 'root of my site')? I have tried both. - Must it also be placed in
blog
? If so, do they need to be identical? - What are the proper contents of the
.htaccess
file(s)?
I have tried all kinds of variations, but can't get it to work – obviously not the right variation yet.
FWIW: I assume my hosting provider uses Apache. I have no control over its configuration.
Best Answer
I would argue you should put your new .htaccess file in public_html folder.
Try the following with mod_rewrite in your .htaccess file
These three lines also have to be added to the .htaccess file in the
blog
folder, modifying theRewriteRule
to reflect that subfolder:It might be also useful to apply mod_ssl to force SSL with the SSLRequireSSL Directive: