1

I have a domain dedicated for my local server (.dev), and an .htaccess file which redirects requests like http://folder.dev/subfolder/ 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 http://folder.dev, but redirects to http://folder.dev/folder/ and consequently throws 404 not found. Similarly, if Parent Directory should link to http://folder.dev/subfolder/, it links to http://folder.dev/folder/subfolder/. Here's how my .htaccess looks like:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_SUBDOMAIN} =""
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]
RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN}]

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

1 Answer 1

0
+100

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 folder1.dev, folder2.dev and all other .dev domains too, and you place your .htaccess right in /htdocs/.htaccess.

When you go to http://folder.dev/subfolder/ (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:// + folder.dev + dirname('folder/subfolder'), that is, of course, equals to http://folder.dev/folder/, and NOT just http://folder.dev/ 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 http://folder.dev/folder/.
  2. We see that "folder" (subdomain) == "folder" (1st path element).
  3. Redirect to http://folder.dev/.

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.
5
  • 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 http://folder.dev. Oct 21, 2013 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 127.0.0.1.
    – cronfy
    Oct 21, 2013 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 127.0.0.1. 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, 2013 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 anythingyoutype.dev already resolves to 127.0.0.1, 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, 2013 at 6:36
  • Great, thanks a million cronfy. You're a legend. Thanks for your time and effort. Oct 22, 2013 at 11:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .