How can the index.html file be served from a sub directory of the DocumentRoot without using system level links?

For example:

  • Let the document root of an apache 2.4 virtual host be dirA
  • Let the the base url (VirtualHost) be http://test.local/
  • Let index.html reside in dirA/dirB/
  • Let http://test.local/ (the base URL) resolve the index.html file (not give a listing of directories).

I've been attempting to use mod rewrite to do this but am unfamiliar with regex and with mod rewrite. The rule I have that works is RewriteRule . /html/index.html [L] but this is unacceptable since any URL will resolve to test.local/html/ (where the index.html file lives).

What is needed is for the exact URL corresponds to the document root find the index.html file in a sub directory of the document root (namely "html") instead of the document root --without using system level symbolic linking (ie: entirely from within Apache).


You may want the mod_rewrite solution that @Stephen suggests, however, also consider DirectoryIndex (depending on your requirements).

The DirectoryIndex document tells Apache which file to serve when you request a directory. This often defaults to index.html in the directory being requested, however, you can specify any file, anywhere. For example:

DirectoryIndex /html/index.html

This instructs Apache to serve the root-relative /html/index.html file when requesting any directory (eg. the document root, as in your case). If you wanted to serve index.html in the requested directory when it exists, but /html/index.html otherwise, then you can include both, in the order that they should be tested, for example:

DirectoryIndex index.html /html/index.html



Your rewrite rule is close. You just need to restrict it a bit more.

RewriteRule ^\/?$ /html/index.html [L]

The \/ matches a literal slash, which is the desired URL for the home page. It is escaped with the backslash.

The question mark makes the slash optional so that the rule can be used in either .htaccess or inside an Apache .conf file.

The ^ matches the start of the URL path and the $ matches the end of the URL path. Putting those on will restrict the rule to just the home page.

If you want to rewrite all .html files to that subdirectory, you could also add the rule:

RewriteCond %{REQUEST_URI} !^/html/
RewriteRule ^\/?(.*\.html)$ /html/$1 [L]
  • "It is escaped with the backslash." - Although there's no need to backslash escape the literal slash here. – MrWhite Feb 26 '18 at 16:55
  • Oh, your last RewriteRule would actually result in a rewrite loop (eg. /html/html/html/foo.html etc.) – MrWhite Feb 26 '18 at 17:14
  • Good catch @MrWhite. I added a RewriteCond to prevent that. I thought the [L] flag might prevent it from being re-evaluated. – Stephen Ostermiller Feb 26 '18 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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