Mobile app version of vmapp.org
Login or Join
Reiling115

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

@Reiling115

Posted in: #Apache2 #Configuration #DocumentRoot #Url #Virtualhost

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 test.local/ Let index.html reside in dirA/dirB/
Let 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).

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Reiling115

2 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

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




Reference: httpd.apache.org/docs/2.4/mod/mod_dir.html#directoryindex

10% popularity Vote Up Vote Down


 

@Heady270

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/ [L]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme