2

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

  • How are these additional domains created in cPanel? Are they "Addon" domains that point to subdomains of the main domain? If so, where do these subdomains point to? ...Subdirectories off the main domain's document root? Or somewhere else on the filesystem? Or are they domain "Aliases" that point to the main domain's document root? How are all these subdomains created? Do you have a "wildcard" subdomain configured (I assume not since you mention that some "don't exist")? Do you have other .htaccess files in subdirectories? – MrWhite Jan 15 at 22:34
  • In the filesystem everything is in public_html, which is the root for sandwiches.org. The other sites are in folders like /bookstore/ in public_html, the only way to have multiple domains on cPanel (at least with as much access as I have right now) is by going to "create addon domain". As for the .htaccess file I only have one big one in public_html. The .htaccess file does work for forcing www. or SSL when going straight to bookstore.com for example. – npbm Jan 15 at 22:45
  • We would need to see the complete "big" .htaccess file in order to rule out conflicts. "www.filmstore.sandwiches.org" - Have you manually created the subdomain(s) www.filmstore (www and filmstore) of the sandwiches.org domain? – MrWhite Jan 15 at 23:01
  • 1
    That is the big .htaccess file in the post, the subdomains were created automatically (when making an addon domain in cPanel). – npbm Jan 16 at 0:08
1

I expect www.filmstore.sandwiches.org to go to sandwiches.org, ...
This does not work, but sends me to filmstore.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.org doesn'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".)

| improve this answer | |
  • I wanna add that I could fix it by adding more .htaccess files, such as in the /bookstore/ folder, but I want to keep it all in the main .htaccess file if it's possible, which I think it should be. I accept that blah.sandwiches.org won't redirect. – npbm Jan 16 at 0:07
  • 1
    Ah, I think I've realised what is going on! I've rewritten / updated my answer. – MrWhite Jan 16 at 1:27

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.