Configure Virtualmin/htaccess to access files via subdomain?

22 posts / 0 new
Last post
#1 Tue, 11/28/2017 - 09:22
Acccent
Acccent's picture

Configure Virtualmin/htaccess to access files via subdomain?

Hi all,

I recently set up a new server, for my wordpress (among other things). The address is robin-v.net; this works correctly.

I've also set up a sub-server, files.robin-v.net – as you can see by going there, it just redirects you to the main blog, and specifically to the Not Found page, which would be correct for any other non-working address. (edit: actually it doesn't do that, it just displays the normal page without changing the URL and with some stuff like fonts not displaying properly because of missing CORS policy...)

My goal is for files.robin-v.net to provide access to (you guessed it) files, so, say, files.robin-v.net/image.png or files.robin-v.net/font.ttf would just lead to the relevant resource. I've put my files in the directory created by Virtualmin, /home/robin-v/domains/files.robin-v.net/public_html/ but I can't find a way to access them.

I don't know if what I'm trying to achieve should be done via .htaccess or an option inside Virtualmin. I couldn't find any option in Virtualmin that seemed related to this. Here is my htaccess, for reference:

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?robin\-v\.net$ [NC]
RewriteRule ^(.*)$ https://robin-v.net/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.robin\-v\.net$ [NC]
RewriteRule ^(.*)$ https://robin-v.net/$1 [R=301,L]


RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

This is in the main server's htaccess. Whatever I put in the sub-server's public_html directory is not accessible, so htaccess files there don't do anything.

Anyone know what I should do at this point? Cheers.

Wed, 11/29/2017 - 00:33
tpnsolutions
tpnsolutions's picture

Hi,

Is your sub-server DNS resolvable?

Best Regards,
Peter Knowles | TPN Solutions
Email: pknowles@tpnsolutions.com | Skype: tpnassist
Wed, 11/29/2017 - 12:30 (Reply to #2)
Acccent
Acccent's picture

Hi Peter, I'm not sure what you mean? I have an A record for files.robin-v.net with the same IP as my main server. Do I need to edit something in my VPS's hosts file?

Wed, 11/29/2017 - 15:16
tpnsolutions
tpnsolutions's picture

Hi,

So it looks like both "robin-v.net" and "files.robin-v.net" resolve, so that's good. When I go to either of the domains, it shows the same page, is that intentional?

Best Regards,
Peter Knowles | TPN Solutions
Email: pknowles@tpnsolutions.com | Skype: tpnassist
Wed, 11/29/2017 - 18:00
Acccent
Acccent's picture

It's not actually the exact same page. Like I said, files.robin-v.net loads my wordpress blog but with some errors, namely all the resources like fonts and images don't show up, because they are in the wp directory. So, to be clear:

  • robin-v.net correctly loads wordpress along with the resources stored in the wordpress directory, which is inside the server's public_html;
  • files.robin-v.net loads wordpress too, I'm not sure why, but because that server's home directory is different, it can't fetch the fonts and images because I have not set up CORS headers;
  • the intended behaviour is that files.robin-v.net throws a 403, and something like files.robin-v.net/card-facebook.png loads that image (which is an actual image currently sitting in /home/robin-v/domains/files.robin-v.net/public_html/)
Tue, 12/05/2017 - 08:08
Acccent
Acccent's picture

does anyone have an idea on how to fix this? :)

Tue, 12/05/2017 - 15:50
tpnsolutions
tpnsolutions's picture

Hi,

If you'd like, we can setup a formal support session and I can diagnose what is going on, and answer any other questions you have. Contact me if you're interested.

Best Regards,
Peter Knowles | TPN Solutions
Email: pknowles@tpnsolutions.com | Skype: tpnassist
Wed, 12/06/2017 - 12:15
Acccent
Acccent's picture

Thanks, I need to do a bunch of stuff on my server and if after all of that it's not resolved, I'll PM you :)

In the meantime, if anyone sees what might be causing this based on the info in the thread, feel free to share! I'll update if I fix it myself.

Tue, 01/16/2018 - 13:23 (Reply to #8)
diegoweb

Hey mate, did you manage to solve your problem? I'm also trying to create a Cookie-Free domain just like you, one sub-domain to point to wp-contents directory.

I'm following this guide: https://www.keycdn.com/support/how-to-use-cookie-free-domains/

But it shows how to create subdomain in cPanel (so much simpler, why can't VirtualMin be as simple as that?) and I have no clue how to do that in VirtualMin.

Tue, 01/16/2018 - 17:54 (Reply to #9)
Joe
Joe's picture

This is the first (somewhat) good argument I've ever heard (in 14 years!) for the way cPanel handles what it calls "subdomains". ;-)

I'll look into adding a feature for this, but I don't want to call it "subdomain" even though that's what cPanel calls it; it confuses too many people. Maybe something like "Sub-directory Domain" (since there's no reason it has to be a subdomain name, and I've always found it utterly bizarre that cPanel conflates the two concepts...you could point any name to a subdirectory, and it's dumb to give people the impression that they have to put subdomain named sites into a subdirectory of the parent domain). There actually is a "subdomain" feature in Virtualmin (at least, I don't think we ever removed it), modeled after the cPanel feature. It's hidden in Virtualmin by default, and I've always hated it because it's really stupidly named. But, I don't think we ever got rid of it, and maybe it's time to consider renaming it and bringing it back into the defaults.

--

Check out the forum guidelines!

Wed, 01/17/2018 - 05:35 (Reply to #10)
diegoweb

Believe me or not, but I went to that "super-hidden-freaking-menu" and enabled the "Sub-domain" stuff. But even with that think enabled, I was having trouble to create a virtual server as "sub-domain". I mean, I was able to create it, but it was not directing to the folder I want.

Some people also complain about "Access Denied" when using this sub-domain stuff. There is something bugged when you have to use this (maybe because it was forgotten and never updated, so it may be using incorrect automation and screwing things up), because you have to set virtual directory manually but Virtualmin keeps switching between Apache and FGCI (I think that's the name), and it screws with files/folders permissions.

Please, take a look at this link and see if you're able to find out what is wrong and if you can fix this "sub-domain" stuff, and maybe even make it enabled by default for some newbies. I know, it may look dumb, but that's why so many webhostings keeps using cPanel, even it's wrong, it keeps things simple (even after 10 years later).

https://www.virtualmin.com/comment/783194#comment-783194

I'm not sure if this is the case that this menu is "broken" (or at least partially broken), but even using this so called "hidden menu" I still can't use it properly like in other webhosting panels (cPanel, VestaCP.....)

Btw: If any english sentence is confusing and you couldn't figure out what I meant, just tell me and I'll try to re-phrase that. I'm a bit rusty in English :P.

Sat, 03/31/2018 - 16:26 (Reply to #11)
diegoweb

Hey buddy, is there a workaround or a ETA to add this feature? :) I haven't heard any feedback since my last reply and I just wanna make sure I can achieve this or I should give up :P

Sat, 03/31/2018 - 21:14 (Reply to #12)
Acccent
Acccent's picture

I personally had to stop using Virtualmin temporarily for unrelated reasons, but before I did I hadn't managed to solve this.

Wed, 02/21/2018 - 08:00
omega1

Hello,

I'm having the same issue.

My websites, for example, client1.domain.com, client2.domain.com, etc... need to access images on cdn.domain.com (hosted on the same server) but I get this error:

Access to Font at 'http://cdn.domain.com/layout/styles/fonts/caviardreams/Caviar_Dreams_Bol...' from origin 'http://client1.domain.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://clietn1.domain.com' is therefore not allowed access.

I have added this as a .htaccess file in the public_html of the cdn.domain.com:

Header add Access-Control-Allow-Origin "*"

but it still presents me with the same error.

Any help would be appreciated.

Thank you.

Sun, 02/24/2019 - 03:33 (Reply to #14)
tsekka

Hi

Did you got this working in the end?

Thanks.

Sun, 02/24/2019 - 10:56 (Reply to #15)
diegoweb

Unfortunately no =(

Mon, 02/25/2019 - 01:57 (Reply to #16)
tsekka

If you need to point sub-domain(e.g. static.yourDomain.com) to some other folder under parents public_html (e.g. wp-content) folder, but you got error while trying to change sub-domains "Website documents sub-directory" under Server Configuration / Website Options, then I got it working like this:

  1. enable sub-domain feature on Virtualmin Configuration,
  2. rename wp-content folder to something different in your public_html (e.g. old-content),
  3. create sub-domain named wp-content.yourDomain.com,
  4. copy-paste content from old-content to wp-content,
  5. if you want, you can change domain name to static.yourDomain.com now (leave home directory unchanged) and you can now serve /wp-content under static.yourDomain.com. You should also request new ssl certificate after changing sub-domains name.

Sounds kind of silly to use this kind of workaround in such a great software as Virtualmin but anyways, this is how i got it working. Hope it works in your case as well.

Mon, 02/25/2019 - 06:51 (Reply to #17)
diegoweb

DUDE! That's awesome.

So the problem about Virtualmin is that it doesn't allow you to create a subdomain to a pre-created folder of the same name. I haven't tested it yet but I will do asap. But one question: Does everything in Wordpress still work as expected? Can you Upload and install plugins from native UI (plugins > install new)?

And by the way, by following your tutorial, will I still be able to manage wp-content from the main domain or do I need to go to subdomains folder (under the main domain) as virtualmin is used to be?

Thank you very much buddy!

---EDIT

I'm facing 2 problems:

1- After creating the sub-domain and setting up the dns on CloudFlare, I'm still unable to visit the sub-domain. Virtualmin does not work well with special characters, so wp-content is not "allowed". When I directly create static.domain.com it works great, even without setting up cloudflare.

2-Well... I'm trying to follow your tutorial but I'm unable to "Change domain name" to static for example. After changing from wp-content.domain.com to static.domain.com, my BIND DNS does not run anymore.

Mon, 02/25/2019 - 09:40 (Reply to #18)
tsekka

Hi

In my case, everything works as expected (don't forget to follow points 3 and 4 of https://www.keycdn.com/support/how-to-use-cookie-free-domains ), on my wordpress site source code i can see that everything previously inside www.domain.com/wp-content gets served under static.domain.com.

You don't actually need to rename wp-content.domain, it does not have to be "static", i think you can probably leave it wp-content (just change define function in wp-config and sql query accordingly). But i did not have any issues while renaming it. Hope removing this renamed sub-domain will get your DNS server working again.

Mon, 02/25/2019 - 10:46 (Reply to #19)
diegoweb

Well, I had to go back and forth with backups and I don't know why, but the last time I was able to create wp-content.domain.com successfully. But following keycdn tutorial was not enough. I had to install BETTER SEARCH AND REPLACE plugin and search for:

www.domain.com/wp-content/

and replace for

wp-content.domain.com/

Now my wp-content folder is being served by the subdomain. Awesome! I will try to rename to static and see if it works. If it don't, I don't mind leaving it as wp-content.

Thank you very much buddy!

EDIT

OK... One problem fixed, but another created. In Wp-Admin some icons are missing. In Chrome Console I'm getting:

"Access to font at 'https://wp-content.domain.com/plugins/woocommerce/assets/fonts/WooCommer...' from origin 'https://www.domain.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."

I will try to figure it out. Did you have this type of problem too?

Wed, 02/27/2019 - 01:52 (Reply to #20)
tsekka

Hi, Replacing www.domain.com/wp-content/ with wp-content.domain.com/ in database was actually point 4 in the keycdn tutorial :)

This is not topic of Virtualmin, but anyway, you need to add cors header. I did not even notice because i dont use woocommerce but one of my plugins css was also not loaded because of missing cors. You just have to addcors header. If you use apache then add next lines to your public_html/.htaccess (found the solution here: https://stackoverflow.com/questions/41036415/cors-with-php-wordpress):

AddType application/vnd.ms-fontobject .eot
AddType application/x-font-ttf        .ttf
AddType application/x-font-opentype   .otf
AddType application/font-woff         .woff
<FilesMatch ".(eot|ttf|otf|woff)">
  Header set Access-Control-Allow-Origin "*"
</FilesMatch>

Solved in my site, hope it works in your site as well.

Tue, 06/11/2019 - 14:17
eugenevdm.host
eugenevdm.host's picture

Hi @tsekka,

Thank you so much. We had a live chat installed on livehelp.domain.com and had problems on our main site and shop.domain.com. By adding your code the problem went away. Just got to love the Virtualmin community, always able to assist.

Eugene van der Merwe

https://vander.host WordPress Website Hosting, VPS Hosting, and Domain Registration