问题原文
原帖链接 · npbm · CC BY-SA 4.0
So I have many domains on one cPanel account, say filmstore.net, bookstore.com and sandwiches.org (the main domain).
So I want to redirect anything.sandwiches.org to sandwiches.org, so I have the following in my .htaccess file:
# Redirect subfolders (even with weird subdomains)
RewriteCond %{HTTP_HOST} ^(.*)sandwiches\.org$ [NC]
RewriteRule ^filmstore/(.*)$ https://www.filmstore.net/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.*)sandwiches\.org$ [NC]
RewriteRule ^bookstore/(.*)$ https://www.bookstore.com/$1 [R=301,L]
# Otherwise, force www.sandwiches.org if it ends with sandwiches.org
RewriteCond %{HTTP_HOST} ^(.*)sandwiches\.org$ [NC]
RewriteCond %{HTTP_HOST} !^www\.sandwiches\.org(.*)$ [NC]
RewriteRule ^(.*)$ https://www.sandwiches.org%{REQUEST_URI} [R=301,L]
# Force www and SSL if naked
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Force SSL otherwise
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
So what does work? Well if I type for example www.filmstore.sandwiches.org/bookstore/ I get redirected to bookstore.com, which is the expected behaviour.
However, I expect www.filmstore.sandwiches.org to go to sandwiches.org, which is what I've checked on the following website: https://htaccess.madewithlove.be/ - but this does not work. Instead, it sends me to filmstore.net, which I think is the default behaviour for multiple cPanel domains.
Also cpanel.sandwiches.org doesn't get redirected at all, nor something that doesn't exist like blah.sandwiches.org. Here it doesn't matter if I end with for example /filmstore/, it doesn't redirect.
My suspicion is that cPanel does something that takes over the .htaccess rules I have in my public_html. How can I solve this?
Running cPanel version 84.0.0.19
已采纳解答
原帖链接 · MrWhite · CC BY-SA 4.0
I expect
www.filmstore.sandwiches.orgto go tosandwiches.org, ...
This does not work, but sends me tofilmstore.net, which I think is the default behaviour for multiple cPanel domains.
Ah, but the "problem" here is that the www.filmstore subdomain maps to the /filmstore subdirectory off the main domains document root (as stated in comments) - this mapping occurs before .htaccess. The RewriteRule pattern then matches against this filesystem path, which is caught by your first redirect directive:
RewriteCond %{HTTP_HOST} ^(.*)sandwiches\.org$ [NC] RewriteRule ^filmstore/(.*)$ https://www.filmstore.net/$1 [R=301,L]
...and triggers the "undesirable" redirect to filmstore.net. Although this is arguably correct behaviour, ensuring both subdirectories and subdomains (associated with the Addon domain) are redirected to the correct domain. To override this and only redirect requests to the subdirectory and not the subdomain then you could add an additional condition that double-checks the requested URL-path. For example:
RewriteCond %{HTTP_HOST} ^(.+\.)?sandwiches\.org\.?$ [NC]
RewriteCond %{REQUEST_URI} ^/filmstore/
RewriteRule ^filmstore/(.*) https://www.filmstore.net/$1 [R=301,L]
I've tweaked the regex so that it only matches optional subdomains, as opposed to anything (which could potentially match other domains like mysandwiches.org etc.). And to allow for an optional trailing dot on the hostname (FQDN).
A request for the filmstore subdomain should not contain /filmstore in the URL-path (ie. REQUEST_URI), so should fall through to the canonical redirect for the sandwiches.org domain instead.
Also
cpanel.sandwiches.orgdoesn't get redirected at all
cPanel generally creates this subdomain as a shortcut to accessing the control panel interface itself. If you only have cPanel access then this is probably out of your control, as it completely bypasses your webspace (and .htaccess file).
nor something that doesn't exist like
blah.sandwiches.org
If a subdomain "doesn't exist" then it won't even resolve as there is no entry in DNS. (?) So any subdomain that "doesn't exist" won't even reach your server. (Unless you have configured "wildcard" subdomains? But then the subdomain arguably "does exist".)
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
