问题原文
原帖链接 · Rick Hellewell · CC BY-SA 3.0
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:
Is there a better way?
Is there a requirement for a separate htaccess in the
somefolder.cfolder (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.
- how best to do this with htaccess files in the public_html folder
- are additional htaccess files needed in subfolders?
已采纳解答
原帖链接 · MrWhite · CC BY-SA 3.0
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.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
