How to set document root to be a subdirectory using .htaccess and not VHost

How to set document root to be a subdirectory using .htaccess and not VHost

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

How to set document root to be a subdirectory using .htaccess and not VHost

When developing locally, you often structure your projects as isolated directories within an htdocs folder. Your local virtual host configuration might look like this:

<VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs/website_1"
    ServerName testpage.com/website_1
</VirtualHost>

This setup works perfectly because the Apache server is explicitly told to serve files from website_1, and your code references paths relative to that specific root. However, moving this setup to a shared hosting environment like JustHost introduces significant constraints. You typically cannot modify httpd-vhosts.conf or httpd.conf to create a new VirtualHost pointing directly to public_html/website_1. Instead, the server defaults to serving your domain from the standard public_html directory.

This mismatch creates a critical issue for relative URL resolution. If your file structure is:

public_html/
    website_1/
        script.php
        style.css

And your script.php contains <a href="/style.css">, the local server serves the link correctly because its DocumentRoot is website_1. On the host, however, Apache attempts to resolve /style.css relative to public_html. Since style.css lives inside public_html/website_1, the request fails with a 404 error.

To bridge this gap without rewriting thousands of lines of code or migrating your entire project structure, you can manipulate the DocumentRoot using .htaccess. This approach allows you to trick the server into treating a subdirectory as if it were the root for URL resolution purposes.

Using RewriteBase and Rewrites to Simulate a Root Change

The core mechanism here is Apache's mod_rewrite module. While DocumentRoot directives in .htaccess are often ignored by older Apache versions or strict security configurations, you can achieve the desired effect by rewriting incoming requests before they reach your application logic.

Create a .htaccess file inside your website_1 directory with the following content:

RewriteEngine On

# Ensure mod_rewrite is available (often required on shared hosts)
Options +FollowSymLinks -MultiViews

# RewriteBase sets the base URL path for relative redirects and rewrites
RewriteBase /website_1/

# Strip "public_html" prefix if it exists in the request path, 
# effectively treating the subdirectory as the root.
RewriteRule ^(.*)$ ../$1 [L,QSA]

Wait—this specific pattern is tricky because Apache serves files from public_html by default. If you simply try to serve website_1/..., the server sees the path relative to public_html.

A more reliable method on shared hosting where you cannot change httpd.conf involves setting the DocumentRoot via .htaccess if your provider permits it, or using a specific rewrite rule that strips the subdirectory name from the URL path.

If your hosting provider allows DocumentRoot in .htaccess, use this:

<Directory "public_html/website_1">
    Options +Indexes +Includes +FollowSymLinks +MultiViews
    AllowOverride All
    Order allow,deny
    Deny from all
    Satisfy Any
</Directory>

# Set the DocumentRoot to the subdirectory
DocumentRoot "public_html/website_1"

If DocumentRoot is ignored in your .htaccess file (which is common on strict shared hosts like JustHost), you must use a rewrite rule that maps the incoming URL path to the physical files, assuming Apache serves from public_html.

The most robust solution when DocumentRoot manipulation fails is to rewrite the request URI so that /style.css becomes website_1/style.css internally. However, this requires your actual file structure to match the requested path relative to the server root. Since you cannot move files on a shared host without FTP access limitations, we must adjust the URL resolution.

The RewriteRule Approach for Shared Hosting

If you cannot change the physical DocumentRoot, you must ensure that requests targeting / are interpreted as requests to /website_1/. You can achieve this by rewriting all non-file requests:

RewriteEngine On
RewriteBase /website_1/

# If a request comes in for /style.css, rewrite it to look for it inside website_1
# This assumes the physical path is public_html/website_1/style.css
RewriteRule ^(.+)$ ../$1 [L]

However, this logic often breaks because Apache strips the subdirectory from the PATH_INFO. A cleaner approach that works on many shared hosts without touching httpd.conf is to explicitly define the rewrite base and ensure internal rewrites handle the path correctly.

Why This Matters for Your Codebase

By implementing these .htaccess rules, you avoid the massive undertaking of refactoring 10,000 lines of code to use ../website_1/style.css. Instead, your application continues to treat website_1 as its root via the / prefix.

Be aware that this method relies heavily on the specific configuration of your hosting provider. Some providers disable mod_rewrite or enforce strict security policies that prevent .htaccess from overriding default directory settings. If you encounter issues where links still fail, it may be necessary to contact support to confirm if DocumentRoot overrides are permitted in .htaccess.

For modern Laravel projects hosted on shared environments, similar logic applies but is often encapsulated within the framework's server configuration or specific middleware that handles path rewriting. For more advanced setups involving Laravel, you might explore how LaravelCompany handles virtual hosting configurations to ensure your application paths remain consistent across different environments.

Ultimately, the goal is to maintain a single source of truth for your file structure. Whether achieved through httpd-vhosts.conf on a VPS or clever .htaccess manipulation on shared hosting, ensuring that the DocumentRoot aligns with your codebase's assumptions is critical for preventing 404 errors in production.

Our network