简体   繁体   中英

htaccess - Force SSL and www with Domain in Subfolder

I'm using a cPanel host, which, unfortunately, does not allow me access to Apache conf files in order to set up any VirtualHosts . I have my domain in a subfolder. I'm now also trying to force SSL as well as forcing www .

My requirements:

  • Domain needs to be in a subfolder, since I also have other domains
  • Force SSL
  • Force www. in the URL

The following is what I have in my .htaccess file:

RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^(www.)example.com$
RewriteRule ^(/)?$ subfolder/index.php [L]

I've made various attempts that seem to get me one or two of my requirements, but not all of them at the same time.

Any help is appreciated.

I was almost there. After, the second line, I had to add a couple of RewriteConds . Not on HTTPS or, not on www:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]

The final set of rules:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
RewriteRule ^$ /subfolder/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1 [L]

Tested, and it forces SSL, and WWW in the domain, and keeps everything in the subfolder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM