duanfeng3879 2014-05-10 17:13
浏览 90
已采纳

访问localhost的Apache 2问题

This is my first post here and I hope I'm doing the right way.

I installed apache2 in my ubuntu 14.04 local machine for studying purposes and all my files are in my home public_html folder.

When I try to access a site like localhost/~{user}/test/index.php everything works fine, but when I try to access the same site with localhost/home/{user}/public_html/test/index.php I get the following error:

The requested URL /home/{user}/public_html/test/index.php was not found on this server.

Apache/2.4.7 (Ubuntu) Server at localhost Port 80

I tried to use:

$echo dirname(__FILE__);

But it returns /home/{user}/public_html/test.

So why can't I access the same file with both paths?

  • 写回答

1条回答 默认 最新

  • dqfwcj0030 2014-05-10 18:11
    关注

    Lets' consider the second URL:

    http://localhost**/home/user/test/index.php**
    

    If you look in Apache's configuration you will notice that there is a directive called "DocumentRoot". This directive specifies where apache will map the initial "/" in the URL to.

    So, let's say that the DocumentRoot is set to /var/www/htdocs. When you request the path /home/user/test/index.php it will actially go looking for .... /var/www/htdocs/home/user/test/index.php, which I'm assuming doesn't exist. See DocumentRoot

    Ok, so why does the first one work? Because of Apache's UserDir module. This little module checks the "/~user/test/index.php" part of the link. The tilde (~) is what does it :). It then maps /~user/ to /home/user/, thus Apache will look for the file at /home/user/test/index.php. Voilá.

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算