【问题标题】:change Apache document root in XAMPP在 XAMPP 中更改 Apache 文档根目录
【发布时间】:2016-01-20 07:17:44
【问题描述】:

我刚刚下载了一个网站,其中包含从主机到我的本地主机 (XAMPP) 的静态 html 页面,并将其保存到 XAMPP htdocs 目录中的子目录中。

当我在浏览器中查看本地网站时,所有 css、js 文件和图像都丢失了。我检查了 HTML 源代码,发现它都使用 css/js 或 href 链接的相对路径。例如:

<a href="/contact">Text Link</a>

它适用于主机,但在我的本地主机上,它指向 http://localhost/contact 而不是 http://localhost/bf/contact(我的子目录名称是 bf)。这个结构也适用于所有的css、js文件,无法加载。

我已尝试将 .htaccess 文件添加到我的子目录:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !bf/
RewriteRule (.*) /bf/$1 [L]

但它仍然无法正常工作。我不想手动更改每个 HTML 页面中的所有 src、href 和链接来解决这个问题。有一种方法可以让 Apache 识别根目录是 /bf 而不是 / ,仅适用于该目录中的所有文件。请帮忙!

非常感谢!

【问题讨论】:

    标签: apache .htaccess document-root


    【解决方案1】:

    您可以通过在本地设置 apache 虚拟主机来完成这项工作,如下所述:

    例如在 ubuntu /etc/apache2/httpd.conf 中, 如果你在 window 上使用 XAMPP,那么 httpd.conf 文件应该是 C:\xampp\apache\conf\httpd.conf。

    请按照以下步骤在本地服务器上配置您的本地站点:

    ------------
    Step 1
    ------------
    i.e 
    <VirtualHost *:80>
      ServerName test.demo.tst ( Any name you want to set )
      DocumentRoot "C:/xampp/htdocs/bf"
       <Directory "C:/xampp/htdocs/bf">
          AllowOverride All  
          Order allow,deny  
          Allow from all
       </Directory>
      #RackEnv development
      ErrorLog C:/xampp/htdocs/bf/logs/test.demo.tst_error
      CustomLog C:/xampp/htdocs/bf/logs/test.demo.tst_access common
    </VirtualHost>
    
    ------------
    Step 2
    ------------
    Than you need to restart apache service
    
    ------------
    Step 3
    ------------
    Than you need to do setting in your hosts files
    for example in hosts file place below entry 
    
    127.0.0.1       test.demo.tst
    
    ------------
    Step 4
    ------------
    Than you can access this url test.demo.tst local server.
    
    I hope this informations would be helpful to you.
    

    【讨论】:

    • 感谢阿图尔的回答。它几乎可以工作。除了当我打开位于本地主机上另一个子目录中的另一个本地站点时,例如 localhost/abc ,然后遇到找不到对象错误。我是不是做错了什么?
    • @LocNguyen 我认为您应该像我上面提到的所有此类本地站点一样使用其他 url 和设置进行配置。
    【解决方案2】:

    请试试这个..

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} !bf/
    RewriteRule ^(.*)$ /bf/$1 [QSA,L]
    

    希望这对您有所帮助。并且你的 apache 重写模式必须在 php.ini 中开启

    【讨论】:

      猜你喜欢
      • 2012-04-26
      • 2012-02-05
      • 2016-06-21
      • 2016-04-17
      • 1970-01-01
      • 2012-08-11
      • 2017-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多