Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk10 min

How to Change Apache’s Document Root Using .htaccess (What Works and What Doesn’t)

An .htaccess file cannot change Apache’s DocumentRoot, but mod_rewrite can route requests into a subdirectory. This guide covers the correct patterns, prerequisites, RewriteBase, front controllers, and troubleshooting.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Short answer: You cannot change Apache’s server-wide DocumentRoot directive from an .htaccess file. You can, however, use mod_rewrite in .htaccess to route requests into a subdirectory, so a site stored in /app can respond to requests received at the current document root. If you control the virtual-host or main Apache configuration and the content really belongs in a different directory, changing DocumentRoot there is the cleaner solution.

The distinction matters: a rewrite changes how a request is mapped, while DocumentRoot changes the filesystem directory Apache serves by default. The examples below show when an .htaccess workaround is appropriate, how to write the rules correctly, and why a rule can appear to do nothing.

As an Amazon Associate I earn from qualifying purchases.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What “document root” means in Apache

Apache’s DocumentRoot is a server or virtual-host setting. It defines the base directory used to locate files for URL paths. If the root is /var/www/site, a request for /images/logo.png is normally looked up under /var/www/site/images/logo.png.

An .htaccess file is different. It is a per-directory override file read only when Apache has been configured to allow it. Its directives can alter request processing in that directory, including URL rewriting with mod_rewrite. It does not replace the virtual host’s DocumentRoot value, move files, or change the directory Apache considers the root for other virtual hosts.

That leads to two separate solutions:

  • Change the real root: edit the virtual-host or main Apache configuration, then reload Apache. This is preferable when all site content has genuinely moved to a new directory and you have server access.
  • Route requests: leave the configured root in place and add rewrite rules in that root’s .htaccess file. This is useful on shared hosting or when a directory-based application must answer requests without changing server configuration.

A rewrite can be internal, keeping the browser’s address unchanged, or it can send a redirect, which changes the visible URL. Most “point the domain at a subfolder” requests need an internal rewrite rather than a redirect.

Choose the right approach before writing a rule

Approach Where it is configured Does the browser URL change? Does the filesystem root change? Best use Main concern
Change DocumentRoot Virtual-host or main Apache configuration No Yes The site has actually moved and you control Apache Requires configuration access and an Apache reload
Internal rewrite in .htaccess Current document-root directory Usually no No Shared hosting or a subdirectory application Incorrect paths can create loops or broken asset URLs
External redirect .htaccess or server configuration Yes No When users should see a new URL such as /app/ Changes bookmarks, canonical URLs, and relative-link behavior
Alias plus rewrite rules Server or virtual-host configuration Usually no No Mapping a URL prefix to a directory outside the root Needs server configuration access; relative substitutions may need RewriteBase

If the content is all in the new directory and you can edit the virtual host, Apache’s own guidance is to change DocumentRoot or move the content rather than preserving a rewrite workaround. Rewriting is a routing technique, not a replacement for a correctly configured root.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Prerequisites for an .htaccess rewrite

A rule is evaluated only if Apache permits it and the rewrite module is available. Confirm these requirements before debugging the expression itself.

Put the file in the active root

The file must be named exactly .htaccess and placed in the directory Apache is actually serving. If the virtual host serves /var/www/htdocs, the relevant file is /var/www/htdocs/.htaccess. Placing it in the intended destination directory does not make it control requests that are handled by a different parent directory.

Allow rewrite directives

The directory configuration must permit at least the FileInfo override class:

<Directory "/var/www/htdocs">
    AllowOverride FileInfo
</Directory>

AllowOverride All also permits the directives, but granting only the classes required by the site is more controlled. Without AllowOverride FileInfo (or AllowOverride All), rewrite directives in .htaccess are silently ignored.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Make sure mod_rewrite is loaded

RewriteEngine, RewriteCond, RewriteRule, and RewriteBase are provided by mod_rewrite. If the module is not loaded, Apache will report an invalid directive rather than process the rule. On managed hosting, the provider may control module loading.

Meet per-directory link requirements

Per-directory rewriting also requires either Options FollowSymLinks or Options SymLinksIfOwnerMatch, subject to the host’s policy. Do not add an option that your provider disallows; ask the administrator to enable the permitted setting in the directory configuration when necessary.

Point the current root at a subdirectory

Suppose Apache’s configured root contains an app directory, and you want requests handled from that directory without changing the visible host name. A basic pattern supplied by Apache documentation is:

RewriteEngine On
RewriteRule ^app(?:/(.*))?$ app/$1 [L]

Place this in the .htaccess file in the current document-root directory. The expression matches app and paths below it, then maps them to the corresponding path inside the app directory.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

There is an important context rule: in an .htaccess file Apache removes the directory prefix before matching. Consequently, the pattern must not begin with a slash. ^/app will not match in this context; use ^app instead.

Understand what this example does

  • The first app is matched against the URL-path portion visible to the per-directory rule.
  • (?:/(.*))? makes the remainder optional, so both the directory path and a path beneath it can be handled.
  • app/$1 is the relative substitution that points into the subdirectory.
  • [L] tells the current rewrite pass to stop after this rule.

Test the mapping with an actual file and a nested path. If the application is meant to answer requests at the site root rather than under /app/, use a root-to-subdirectory rule designed for that URL shape instead of blindly adapting this example. A rule that repeatedly rewrites an already rewritten path can produce an internal redirect loop, so inspect the logs whenever the response becomes a 500 error.

Use the correct pattern for a front-controller application

Many PHP and other web applications send every request that is not a real file or directory to one entry point such as index.php. For an application installed in a subdirectory, Apache documents this per-directory pattern:

RewriteEngine On
RewriteBase /myapp/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]

How the conditions protect assets

%{REQUEST_FILENAME} represents the filesystem path Apache has resolved for the current request. The !-f condition allows an existing file, such as a stylesheet or image, to be served normally. The !-d condition does the same for an existing directory. Only requests that are neither real files nor real directories are sent to index.php.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why the application prefix matters

The value in RewriteBase is the URL prefix for the application in this example. The rule itself is written without a leading slash because it runs in per-directory context. Adjust both the directory placement and URL prefix to match the application’s actual installation.

Apache notes that FallbackResource can be simpler and more efficient for this particular front-controller use case. It is worth considering when the application needs a straightforward fallback and does not require the conditional rewrite logic above.

Understand RewriteBase before adding it

RewriteBase supplies the URL prefix Apache uses when it turns a relative substitution back into a URL path. It matters when the URL path and filesystem path do not line up, such as with an Alias, a symlink mapping, or another boundary between URL space and filesystem space.

It is not a universal fix for a failed rule. If the URL and filesystem paths already correspond, adding a guessed base can make the result harder to understand. Absolute URL-path or full-URI substitutions do not use RewriteBase.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Decide whether the paths differ

  1. Write down the URL the visitor requests, for example /myapp/orders/7.
  2. Write down the filesystem directory Apache must use, for example a directory represented by an alias rather than a child of the current root.
  3. If those paths share the same per-directory relationship, start without RewriteBase.
  4. If a relative substitution crosses the mapping boundary, set RewriteBase to the URL prefix that represents the target directory.

Do not use a leading slash in the RewriteRule pattern simply because RewriteBase contains one. They serve different purposes.

Internal rewrite versus redirect

Internal rewrite

An internal rewrite changes Apache’s lookup while leaving the address bar unchanged. It is appropriate when the public URL should remain stable but the files live below a subdirectory. Because the browser does not see the internal path, application links, cookies, and canonical URLs must still be configured for the public URL you intend to expose.

External redirect

A redirect tells the browser to request another URL. Use one when the subdirectory should be visible, such as moving visitors from the root to /app/. The visible URL changes, so update links, bookmarks, canonical metadata, and any path-sensitive application settings accordingly.

Do not add a redirect merely because an internal rewrite feels less intuitive. Redirecting a request to the same path that a rule is already processing can create a loop, and redirecting every asset request to a directory index can break the site.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why a RewriteRule appears to do nothing

Work through these checks in order. Each one corresponds to a common failure mode in per-directory rewriting.

1. Verify the active directory

Confirm which virtual host answered the request and which directory it serves. An .htaccess file in an unused project directory has no effect on another virtual host. Check the host’s configuration or ask the administrator for the active root.

2. Check AllowOverride

Ask for the <Directory> block governing that root and confirm it has AllowOverride FileInfo or AllowOverride All. If overrides are disabled, Apache ignores rewrite directives instead of partially applying them.

3. Confirm mod_rewrite

Make sure the server has loaded mod_rewrite. A missing module commonly produces an “Invalid command” error for RewriteEngine; a host may need to enable it centrally.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

4. Remove the leading slash from the pattern

In .htaccess context, use:

RewriteRule ^app/(.*)$ app/$1 [L]

not:

RewriteRule ^/app/(.*)$ app/$1 [L]

Apache strips the directory prefix before matching, so a pattern beginning with ^/ never matches there.

5. Check the substitution and base

Determine whether the substitution is relative to the current directory and whether the URL-to-filesystem mapping includes an alias or symlink. Add or correct RewriteBase only when that mapping requires it. An incorrect base can send Apache to a path that does not exist.

6. Look for loops and forbidden processing

Inspect Apache’s error log after reproducing the request. Repeated internal redirects usually mean the rule matches its own rewritten result or a separate rule sends the request back. A forbidden rewrite error can indicate that the server’s override or symlink policy does not permit the requested operation.

7. Test one request at a time

Start with a known static file, then test a nested path, then test the application’s front controller. Clear any browser cache only after the server-side mapping is correct; caching can conceal whether the newest rule ran.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Common mistakes and their corrections

Symptom Likely cause Correction
The rule never matches The pattern starts with ^/ Remove the leading slash in the RewriteRule pattern.
Every request returns the old site The file is not in the active document root, or overrides are disabled Verify the serving directory and AllowOverride.
Images and CSS fail Requests for real assets are being sent to the front controller Preserve existing files and directories with !-f and !-d conditions.
Apache reports an internal redirect loop The substitution matches the rule again, or two rules point at each other Inspect the rewritten path, narrow the pattern, and review the error log.
Relative links point to the wrong location The public URL prefix and filesystem path differ Review the application’s base URL and use RewriteBase only where the mapping requires it.
Apache returns a 500 error after editing Invalid directive, unavailable module, or disallowed option Read the error log, validate the directive spelling, and have the administrator check module and override settings.

When changing DocumentRoot is the better fix

Use the server configuration when the new directory is the site’s real home, not merely an implementation detail. A direct root change avoids an extra rewrite pass, reduces the chance of relative-path mistakes, and makes the virtual host’s intent explicit. It also prevents unrelated directories under the old root from being accidentally exposed through broad rules.

The trade-off is access and coordination. You need permission to edit the virtual-host or main Apache configuration, and the change normally requires a reload or restart according to the host’s operating procedure. On shared hosting, those controls are often unavailable; in that case, a carefully scoped .htaccess rewrite may be the only practical option.

Use this decision checklist

  • Choose DocumentRoot when all content belongs in the destination directory and you control Apache’s virtual-host configuration.
  • Choose an internal rewrite when the public URL must stay the same, the application is below the current root, or hosting permits only .htaccess.
  • Choose a redirect when the destination prefix should be visible to visitors.
  • Choose an Alias with server configuration when a URL must map to a directory outside the normal root.
  • Use a front-controller rule when only non-file and non-directory requests should reach an application entry point.

A safe rollout procedure

  1. Back up the existing file. Save the current .htaccess before adding rules so you can restore a known-good version.
  2. Map the paths. Record the public URL, current document-root directory, destination directory, and application entry point.
  3. Confirm server prerequisites. Check AllowOverride, mod_rewrite, and the permitted symlink option.
  4. Add the smallest rule that fits. Do not combine a subdirectory mapping, redirect, and front-controller fallback until each works independently.
  5. Test static content first. Request a real file and verify that it is served from the intended directory.
  6. Test an application route. Request a nested, non-file URL and confirm that the correct entry point handles it.
  7. Inspect logs immediately. Resolve loops, forbidden processing, and missing-file errors before adding more conditions.
  8. Document the public path. Tell the application which URL prefix it should generate for links, forms, cookies, and redirects.

Keep the final rules narrow and comment why the mapping exists. A later administrator should be able to distinguish an intentional subdirectory route from an obsolete migration rule.

Key points to remember

  • .htaccess cannot redefine Apache’s server-wide DocumentRoot.
  • mod_rewrite can route requests into a subdirectory without changing the browser URL.
  • Per-directory patterns omit the leading slash because Apache strips the directory prefix before matching.
  • AllowOverride FileInfo or AllowOverride All is required for rewrite directives in .htaccess.
  • RewriteBase is for relative substitutions when URL and filesystem paths differ, particularly across aliases or symlink mappings.
  • If the content truly belongs in a new root and you control Apache, change DocumentRoot directly instead of preserving a rewrite workaround.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.