I want to host three different applications, we call them web, admin and api on a shared hosting which an apache web-server. The domain is called www.example.com.
www.example.comshould point to the web appwww.example.com/adminshould point to the admin appwww.example.com/apishould point to the api app
For better maintenance, I want those three apps within different folders on the server. My dream folder structure would be:
/ ......................... root of the server
/www_test_folder/web ...... web app; www.test.com points to it
/www_test_folder/api ...... api app; www.test.com/api points to it
/www_test_folder/admin .... admin app; www.test.com/admin points to it
I tried some .htaccess configurations but without any success; so my general question ...
Is this even possible? And if yes, how would the .htaccess will look like?
I'm assuming
/www_test_folderis yourDocumentRoot- the directory to which the hostnamewww.example.comalready points to.You shouldn't need to do anything specific for the URLs/routes
/adminand/api(other than perhaps creating an exception) since these point directly to the subdirectories of the same name. But since these map to physical subdirectories, the root "canonical" URL of these apps must include a trailing slash. ie./admin/, not/admin(otherwise mod_dir will issue a 301 redirect to append a trailing slash).That just leaves you to implement an internal rewrite for the "web app" to internally rewrite requests from the root to the
/websubdirectory. This can be achieved in the root.htaccessfile (although exactly how this is implemented can depend on the apps themselves and whether they have their own.htaccessfile in the relevant app subdirectory).For example:
The filesystem checks (the first two conditions) may or may not be necessary, depending on how the sub apps are configured. In fact, if each app has its own
.htaccessfile (in the relevant subdirectory) that uses mod_rewrite (a front-controller pattern or canonical redirect for instance) then the above rule could potentially be reduced to a simple one-liner. For example:The presence of an
.htaccessfile (containing mod_rewrite directives) in the subdirectory prevents erroneous rewrites that could otherwise result in a rewrite-loop. This is because mod_rewrite is not inherited by default.You then need to prevent direct/user access to the
/websubdirectory. Although this subdirectory should be entirely hidden from the user, if it did get inadvertently exposed it could cause a duplicate content issue. This means that any assets (images, CSS, JS, etc.) should not reference the/websubdirectory publicly.This could be achieved in the same root
.htaccessfile (if the web app did not itself have its own.htaccessfile), however, it would be preferable to do this in the web app's own.htaccessfile at/web/.htaccess.For example:
The
RewriteRulepattern matches against the relative URL-path, the part after the/web/prefix. The check against theREDIRECT_STATUSenv var ensures that only direct requests from the client are caught and not internally rewritten requests by the rewrite in the root.htaccessfile.Alternatively, use subdomains
However, having said all the above, if these are 3 entirely separate apps then it would arguably be preferable to use subdomains instead (this should be possible in most shared server environments).
You would have two subdomains:
admin.example.comandapi.example.com. These subdomains need to point to two subdirectories within your shared hosting account. However, these subdirectories do not need to be (and should not be) within the main domains public HTML space (document root), they can point to subdirectories outside of the main domains document root (even in a shared server environment). (Otherwise, you potentially have a duplicate content issue again, which requires additional rules to resolve.)The web app would simply be in the main domains document root, not in a
/websubdirectory.This enables the 3 apps to be entirely separate. No additional rewriting/
.htaccessfiles are necessary.