0

I want my subdomain subdomain.example.com to load content from a subfolder on the root domain example.com/subfolder.

So if a user goes to subdomain.example.com/?article=hey it should load the content from domain.com/subfolder/?article=hey.

Web server: Apache

2
  • What web server? If Apache, this should be possible. Your example, domain.com/subfolder?article=hey may better appear as domain.com/subfolder/?article=hey. Is that what you want? If both are on the same server with Apache, you can change DocumentRoot and Directory directives of the sub-domain to point where ever you want. As well, you can always use a symlink so that the DocumentRoot and Directory of the sub-domain are in fact what you want. I prefer the first method of course.
    – closetnoc
    Commented Apr 17, 2016 at 14:14
  • How are you creating the subdomain? Are you using a hosting control panel of some kind eg. cPanel?
    – MrWhite
    Commented Apr 17, 2016 at 15:20

1 Answer 1

3

If you are using Apache then you can simply create an additional VirtualHost for your sub domain and set the webroot to the directory you choose.


Example:

Your current VirtualHost is most likely something along the lines of the following; with the ServerName your domain and the DocumentRoot being the web root.

<VirtualHost 1.2.3.4:80>
    ServerName www.example.com
    DocumentRoot "/www/domain"
</VirtualHost>

You could create another VirtualHost with the ServerName as the sub domain and the DocumentRoot as the sub folder you want to use such as this:

<VirtualHost 1.2.3.4:80>
    ServerName subdomain.example.com
    DocumentRoot "/www/domain/subfolder"
</VirtualHost>

Now when you access subdomain.example.com in your browser it would load the content from /www/domain/subfolder. Keep in mind that in this example you would still be able to access the sub folder by going to www.example.com/subfolder since the sub folder is still within the first VirtualHosts DocumentRoot.

3
  • Seems like a good solution, could that be done in .htaccess as well for a solution that could be moved easily with a site when moved to other hosts et cetera? Commented Apr 17, 2016 at 14:20
  • 2
    VirtualHosts can not be done with htaccess it would have to be done within the apache conf. file. But if you need to use htaccess you might be able to do this with a redirect if you have a wildcard DNS setup or just make sure that the subdomain is also pointed to the same address. Here is a question on SO that might get the results you want - stackoverflow.com/q/10642426/2268476
    – Analog
    Commented Apr 17, 2016 at 14:59
  • 1
    @PetaspeedBeaver "a solution that could be moved easily with a site" - The creation of the subdomain in the first place isn't necessarily something that can be "moved easily with a site". Incidentally, on hosts that use cPanel then this is the default behaviour when you create a subdomain.
    – MrWhite
    Commented Apr 17, 2016 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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