skip to Main Content

I occasionally make simple HTML websites with a handful of pages that later get passed along to developers that flesh them out with more sophisticated functionality. Is there an easy way — possibly with .htaccess — to throw the files up to an arbitrary directory on my shared web hosting, and have that directory be the document root for that site? It would just be a temporary location that would allow another person to see a preview of the site before the files are passed along to someone else.

I typically create a head.php file that loads Bootstrap, the CSS stylesheet, some Google fonts, and some Javascript. I also have a menu.php file and a footer.php file. These are all referenced in the content files with PHP includes so I only have to write that code once. The problem is, unless all the files are in a single directory, the relative links break (particularly the link to the CSS). So I would like to be able to link to those PHP files with an absolute link (for example /head.php), but have that / refer to the arbitrary site directory instead of the root directory for the entire hosting account. What’s the easiest way to do this?

2

Answers


  1. What you ask is not possible in a direct manner for obvious reasons … How should the http server know in what folder it should look for such a distributed configuration file (".htaccess")? That would mean pulling yourself out of the swamp by your own hair …

    There are solutions for this, though, at least something very close. Actually multiple options. Here are some ideas:

    You use the apache default host. Each apache http server has such a default host if it is setup to serve virtual hosts (so pratically all installations). It always is the first define http host found in the server configuration. That one will respond to any request where no specialized host exists for (checked via the http host header in the request). Now you have a single folder in your file system that is used as a DocumentRoot for that http host. That means you have two options now:

    You can use a top level configuration file in that folder where you create a rewriting rule for each such customer website you have. That rule rewrites the requests to a folder with the name of the requested http host (or similar). So something like that:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www.)?example-a.com$
    RewriteCond ^ /example-a{REQUEST_URI} [L]
    RewriteCond %{HTTP_HOST} ^(www.)?example-b.com$
    RewriteCond ^ /example-b{REQUEST_URI} [L]
    RewriteCond %{HTTP_HOST} ^(www.)?example-c.com$
    RewriteCond ^ /example-c{REQUEST_URI} [L]
    

    Or you obviously can generalize that to a generic rule:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(?:(www.)?)([^.]+.[^.]+)$
    RewriteCond ^ /%1%{REQUEST_URI} [L]
    

    You might want to add a condition that checks for the existence of the target folder maybe. And a fallback content in the main folder that is served if no matching folder is found.

    An alternative would be the solutions for mass virtual hosting. You will find examples for that online. By most likely that is too much effort for the simple use case.

    I personally would advise against all such things, though. In my eyes the effort to simply copy a template vhost configuration is so trivial that it should not post a hurdle. And it offers a ton of additional options and security.

    Keep in mind: for all such approaches you need working DNS resolutions. Either by registering domains and configuring DNS entries for those (however you do that). Or by using a wildcard DNS record in combination with a subdomain per customer / site, so not a real, separate domain. If you go for subdomains, then above rules would probably look something like that:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^(?:(www.)?)example.com$
    RewriteCond %{HTTP_HOST} ^([^.]+.)example.com$
    RewriteCond ^ /%1%{REQUEST_URI} [L]
    

    A simpler approach obviously would be to simply use subfolders for all customers / sites. Not beautiful, but hey, it is test sites, right?

    Login or Signup to reply.
  2. To set the document root for a website using .htaccess, you can use the RewriteRule directive in combination with RewriteCond. Here’s a concise example

    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/subdirectory/(.)$
    RewriteRule ^(.
    )$ /subdirectory/$1 [L]

    This configuration will rewrite all requests to point to the specified subdirectory within your website, effectively setting a new document root. Make sure the mod_rewrite module is enabled in your Apache configuration.

    Connect us for coding related problem. We are Software Development Company Deals in App development, Software Development and Digital Marketing too.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search