开发者

.htaccess make folder act as root?

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 开发者_Python百科in sub-folders.


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.


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.


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

<a href="/index.php">Link</a> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜