1

The issue is how to do two things: force SSL on all requests, and redirect the domain to the public_html/somefolder - where the site/domain content lives. The recommendation from the hosting place (BlueHost, although I have seen this elsewhere, is to do this second thing via htaccess in the public_html folder:

RewriteEngine on
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

#this next section is what the hosting place says to use    
# .htaccess main domain to subdirectory redirect
# Do not change this line.
RewriteEngine on
# Change example.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/somefolder.c/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /somefolder.c/$1
# Change example.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ somefolder.c/index.php [R,L] 

Note that the site content lives in the somefolder.c folder.

So the htaccess should take care of these requests

www.example.com
example.com
http://example.com
https://example.com
http://www.example.com
https://www.example.com
https://www.example.com/somepage.php   #this page is in public_html/somefolder.c
http://example.com/folderb/somepage.php  #this page is in public_html/somefolder.c/folderb 

...and variations therein.

I'm not sure that the htaccess above will work in all of those situations. So:

  1. Is there a better way?

  2. Is there a requirement for a separate htaccess in the somefolder.c folder (the content 'root')? If so, what should it contain?

I'd prefer not to rearrange the whole place (moving the site files from public_html/somefolder.c to the public_html folder.

Added

There are several domains associated with this hosting account. The primary domain is www.example.com .

I note that the 'first force SSL part' is working. And the intent of the second part is to have https://www.example.com point to the somefolder.c content, with https://www.example.com/folderb/somepage.php point to the public_html/somefolder.c/folderb/somepage.php file.

That https://www.example.com/folderb/somepage.php request will (sometimes; not consistently) show https://www.example.com/somefolder.c/folderb/somepage.php on the address bar. I want the https://www.example.com/folderb/somepage.php to show on the address bar. So the 'somefolder.c' should never appear on the address bar.

  1. how best to do this with htaccess files in the public_html folder
  2. are additional htaccess files needed in subfolders?
  • 1
    Is the force to SSL working? – Steve Jul 5 '17 at 8:49
  • 1
    What actually does not "work" - what happens at the moment? Just to clarify, /somefolder.c should be entirely hidden from the user? So, to the end user it just looks like all the files are served from the document root? It's just that you have a mixture of internal rewrites and external redirects to this subdirectory - which I suspect is incorrect. Are you hosting more than one domain on this account? Presumably you don't yet have any other .htaccess files? Do you need to be able to serve any files from the document root? – MrWhite Jul 5 '17 at 9:08
  • Please edit the question to clarify as requested by the comments. – Stephen Ostermiller Jul 5 '17 at 14:03
  • Question edited per request. – Rick Hellewell Jul 5 '17 at 18:26
  • I removed "redirect" from the title. From your explanation you don't want redirects. Redirects change the URL and make that change visible to users. You want a "rewrite" that internally changes how the server fulfills the request. – Stephen Ostermiller Jul 6 '17 at 0:47
1

There are several domains associated with this hosting account. The primary domain is www.example.com

It's a bit unclear (from your code) what you want to do with these other domains? Do you wish to canonicalise all these domains to your "primary domain", or do you want to keep these separate (to perhaps maintain different content on the different domains)? Your HTTP to HTTPS redirect (which you say is "working") redirects everything to www.example.com - so this canonicalises all domains to the primary domain. However, all the rules that follow explicitly check for this domain in the request, as if multiple domains are expected - this would seem to be unnecessary?

So, I'll assume these domains should be canonicalised. I'll also assume that non-www/www should also be canonicalised (which you are not currently doing).

RewriteRule ^(/)?$ somefolder.c/index.php [R,L]

Also, your last rule issues an external redirect to somefolder.c/index.php - this will break (unless you've defined a RewriteBase somewhere else?). But this should no doubt be an internal rewrite, not an external redirect. However, its doubtful whether this is required at all.

Try something like the following instead (based on your current ruleset):

RewriteEngine on

# non-www to www redirect (HTTP and HTTPS)
#  - Also canonicalises all other domains
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) https://www.example.com/$1 [R,L]

# HTTP to HTTPS redirect
# HTTP_HOST is guaranteed to be the primary domain by this stage
RewriteCond %{SERVER_PORT} 80 
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]

# Rewrite all requests (for non-existent files) to `/somefolder.c`
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^somefolder\.c /somefolder.c%{REQUEST_URI} [L]

The check for non-existent files and directories may not be required - in fact this may not be desirable at all. This allows for existing files (and directories) to be served outside of the /somefolder.c directory. However, it also prevents the site contained within the subdirectory from serving its own custom 404 ErrorDocument.

Change the above temporary (302) redirects to 301 only when you are sure it's working OK. ie. change R to R=301.

Make sure your browser cache is clear before testing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.