Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a domain dedicated for my local server (.dev), and an .htaccess file which redirects requests like to /htdocs/folder/subfolder. It works great and all, except for one minor issue. When I have the Directory Listing enabled, I can access all the folders, subfolders and files properly, except when I click on the Parent Directory link, which, for example, should lead to, but redirects to and consequently throws 404 not found. Similarly, if Parent Directory should link to, it links to Here's how my .htaccess looks like:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9][-a-z0-9]+)\.dev\.?(:80)?$ [NC]
RewriteCond %2 !^www|ftp|mail|pop3|localhost$
RewriteCond %{DOCUMENT_ROOT}/%2 -d
RewriteRule ^(.*) %2/$1 [E=SUBDOMAIN:%2,L]

Apart from that one thing, everything else works fine (e.g. relative links in documents etc.)

share|improve this question
up vote 0 down vote accepted

This happens because when Apache shows directory index, it evaluates "Parent Directory" link depending on DocumentRoot of virtual host and current directory.

I guess you use /htdocs directory to be the document root of, and all other .dev domains too, and you place your .htaccess right in /htdocs/.htaccess.

When you go to (document root is /htdocs/), your .htaccess shows you /htdocs/folder/subfolder/ instead. Relative path from document root to this directory is folder/subfolder. When Apache calculates parent directory link of a directory it currently displays, it takes http:// + + dirname('folder/subfolder'), that is, of course, equals to, and NOT just as you would expect.

It would be possible to check in .htaccess, if first path element after domain name equals to subdomain and redirect to root:

  1. We have
  2. We see that "folder" (subdomain) == "folder" (1st path element).
  3. Redirect to

But if you do this, it will not be possible to access paths like /htdocs/folder/folder/, where first path element is indeed the same as subdomain name.

Given that, you can not solve this problem with just .htaccess and mod_rewrite.

Another solutions:

  1. Use vitrual host for any folder you'd like to access. No .htaccess magic required. Vhost configs can be generated from folders list. Most flexible solution, as it is possible to create any vhosts, with any config options, for any folders wherever they are.
  2. Use mod_vhost_alias. Easier, does not require scripting skills, but may require changes in folder naming for automatic conversion between folder_name and document_root.
  3. Use HeaderName in .htaccess to add header to all index pages. Use jQuery to change parent directory link in any way you wish. Looks like a dirty hack, but will work though.
share|improve this answer
cronfy, thanks for the detailed answer. I wasn't aware that vhost can be managed dynamically, I thought just using .htaccess would be simpler and safer. What I've learned is that "Adding virtual hosts is simply a matter of creating the appropriate directories in the filesystem and entries in the DNS", so I guess I should still direct new domain names to my localhost manually, right? If so, then it still doesn't solve my need to just create a folder and have it available at – Томица Кораћ Oct 21 '13 at 7:37
Of course you have to add a dns record. How do you create it now? If you are using /etc/hosts, you can generate it from folders list. And if you are using some dns server, you can create a wildcard for *.dev to point all subdomains of .dev to – cronfy Oct 21 '13 at 9:07
Sorry, I think you misunderstood me. Of course I have a DNS record, but as you've mentioned, I have a *.dev directed to What I wanted to ask was will such wild-card DNS setting work with dynamic vhosts, or will I have to direct each new vhost domain manually in my DNS server settings? – Томица Кораћ Oct 21 '13 at 22:29
DNS and webserver are completely independent systems. DNS is used to convert domain name to IP address. Then your browser sends a request to this IP. Apache does need to know how your browser has reached him. If already resolves to, than you are done with the DNS part. In other words, no, you do not need to change DNS setup whether you use .htaccess, mod_vhost_alias or normal vhosts. On the Apache side, especially if you will use mod_vhost_alias, you may have need to change folder names to ease folder_name -> doc_root conversion and Apache configuration. – cronfy Oct 22 '13 at 6:36
Great, thanks a million cronfy. You're a legend. Thanks for your time and effort. – Томица Кораћ Oct 22 '13 at 11:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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