Search code examples
apache.htaccessdocument-root

change Apache document root in XAMPP


I have just downloaded a website which contains static html pages from a hosting to my localhost (XAMPP) and saved it to a subdirectory in XAMPP htdocs directory.

When I view the local website in browser, all css, js files and images were missing. I checked the HTML source code and saw that it's all using relative path for css/js or href link. For example:

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

It works on the hosting, but on my localhost, it points to http://localhost/contact instead of http://localhost/bf/contact (my subdirectory name is bf). This structure is applied to all css, js files too and they could not be loaded.

I have tried to add .htaccess file to my subdirectory:

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

But it still not works. I don't want to manually change all src, href, and link in every HTML pages to fix this. There would be a way to make Apache recognize the root is /bf instead of / , applied to all files in this directory only. Please help !

Thank you very much !


Solution

  • You can get this work by setting apache virtual host at locally as describe below:

    For example in ubuntu /etc/apache2/httpd.conf, If you are using XAMPP on window that is the case, then the httpd.conf file should be C:\xampp\apache\conf\httpd.conf.

    Please follow below steps to configure your local sites at local server:

    ------------
    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.