【问题标题】:Apache virtual host uri with a dir带有目录的 Apache 虚拟主机 uri
【发布时间】:2013-01-29 01:50:30
【问题描述】:

我无法使用子目录在 url 上设置虚拟主机... 我需要在这样的地址上运行我的项目:

http://www.projects.loc/project1/ 这应该模拟安装在一个 Web 服务器上的地址将是这样的

http://www.someServer.com/projects/project1/

我需要将重定向调整为“/”,以便返回到 www.projects.loc/project1/

在 hosts.txt 我有:

127.0.0.1       www.projects.loc

vhosts 已启用,httpd-vhosts.conf 如下所示:

NameVirtualHost *:80

<VirtualHost *:80>
    DocumentRoot "D:/Projects/Project1/public/"         
    ServerName  www.projects.loc/project1/
</VirtualHost>

我错过了什么?

编辑: .htaccess 看起来像这样:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php [NC,L]

应用程序在干净的域上正常运行,但我无法将其配置为 在 domain.com/some_dir/ 上运行

编辑:
解决了这个!

NameVirtualHost *:80   

   <Directory "D:/Projects"> 
      allow from all
   </Directory>

    <VirtualHost *:80>
        DocumentRoot "D:/Projects"      
        ServerName  www.projects.loc/

        Alies /project1  /Project1/public/
    </VirtualHost>

注意:这是仅适用于开发环境的最小配置,
检查来自@M-z 的接受的答案以获取生产环境的完整详细信息。

【问题讨论】:

  • 你尝试过哪些重写?

标签: php apache vhosts


【解决方案1】:

您可能已经解决了这个问题。无论如何,我今天正在寻找类似的东西,因此我在这里记录了解决方案:

你不想在 ServerName 中写斜杠

服务器名称 www.projects.loc/project1/

如果您只有一个名为“project1”的项目,您可以使用“ServerPath”轻松完成工作,您的虚拟主机配置将如下所示:

<VirtualHost *:80>
  ServerName projects.loc
  ServerAlias www.projects.loc
  ServerPath /project1/
  DocumentRoot /PATH/public_html
  ErrorLog /PATH/error_log
  CustomLog /PATH/access_log combined
  DirectoryIndex index.html index.htm index.php index.php4 index.php5

  <Directory /PATH/public_html>
    Options -Indexes +IncludesNOEXEC +FollowSymLinks
    allow from all
  </Directory>

</VirtualHost>

通过 ServerPath,您可以将目录挂载到 projects.loc/project1

无论如何,假设您有多个项目(project1、project2)要绑定到 projects.loc/project1、projects.loc/project2 等,请使用“别名”。您的虚拟主机配置文件应如下所示:

<VirtualHost *:80>
  ServerName projects.loc
  ServerAlias www.projects.loc
  DocumentRoot /PATH/public_html
  ErrorLog /PATH/error_log
  CustomLog /PATH/access_log combined
  DirectoryIndex index.html index.htm index.php index.php4 index.php5

  <Directory /PATH/public_html>
    Options -Indexes +IncludesNOEXEC +FollowSymLinks
    allow from all
  </Directory>

  Alias /project1 "/PATH/public_html/project1"
  <Directory "/PATH/public_html/project1">
    DirectoryIndex index.html index.htm index.php index.php4 index.php5
    Options -Indexes +IncludesNOEXEC +FollowSymLinks
    allow from all
  </Directory>

  Alias /project2 "/PATH/public_html/project2"
  <Directory "/PATH/public_html/project2">
    DirectoryIndex index.html index.htm index.php index.php4 index.php5
    Options -Indexes +IncludesNOEXEC +FollowSymLinks
    allow from all
  </Directory>

</VirtualHost>

然后,您在文件夹 /PATH/public_html/project1 中的应用程序将在 projects.loc/project1 中可用,您在文件夹 /PATH/public_html/project2 中的应用程序将在 projects.loc/project2 中可用,依此类推。

我宁愿为不同的应用程序使用不同的子域。这样做的好处是每个子域主机都有自己的配置文件,这也使得错误和访问日志处理更容易。通过使用别名,如果您想为每个应用程序配置不同的错误和访问日志,将更加困难。

进一步阅读:
关于别名:http://httpd.apache.org/docs/current/mod/mod_alias.html
关于ServerPath:http://httpd.apache.org/docs/2.2/vhosts/examples.html#serverpath

【讨论】:

  • 我现在解决了这个问题,当我查看你的回答时..它是正确的,但很详细..所以我第一次没有得到它:)
猜你喜欢
  • 2012-01-27
  • 2012-04-29
  • 2015-12-03
  • 2015-01-31
  • 1970-01-01
  • 2018-07-05
  • 2015-04-09
  • 2014-12-24
  • 1970-01-01
相关资源
最近更新 更多