views:

43

answers:

3

Hi,

Is there a way to make .htaccess tell a folder to act as the lowest level? What I mean is this, say you have a folder like so:

/about/
/contact/
/css/
/images/
.htaccess
index.php
header.php

If they are at /contact/index.php, then if I have <a href="../index.php">Home</a> to go to the home page, it works all fine, but if they are on the actual main page, it will try to go a directory lower.

The reason is because Im trying to test sites in sub-folders.

+3  A: 

The directly answer your question, the answer is no. By the time, .htaccess is read, the path was already translated to a directory.

If you want to change the root, you have to do it in httpd.conf (or rewrite all the requests in the root to the destination directory). If you want to have several roots (under different domains), you have to use virtual hosts (again, only in httpd.conf).

You can also make your site work when it's not in the root directory.

  • Use relative paths correctly ("../index.php" in "/contact/index.php" and "index.php" in the root webpages). Not a great option.
  • Use absolute paths, but prefix them with the path of the website relative to the server root. The most fool-proof way to do this setting a constant for this in a file that you include in all your scripts.
Artefacto
+1  A: 

This is a bit hacky... but you could create the /home folder and put a duplicate copy of your index.php in there. Again, it is a total hack, but it should resolve what you are trying to do.

Another thing to consider is using mod_rewrite in the .htaccess to look for /home and route to the root directory.

cdburgess
A: 

On /contact/index.php you can link to main index.php by using

<a href="/index.php">Link</a> 
Chris
It wasn't super-clear from the question, but the reason he can't do that because he's testing the site in a subdirectory, meaning the target for the link would have to be "/subdir/index.php" (which wouldn't match the production environment).
Tim Stone
So use a variable defined as siteRoot = "/subdir/"; and then any link or reference you use that.
Chris