Search code examples
phpabsolute-path

How to make local server treats a subfolder as the root folder?


When I'm developing my own website on my local server, I put its files in a subfolder. For example, the local url looks something like this:

http://127.0.0.1/projects/myownwebsite/

After I upload its files onto the server of a web hosting company, they become in the root folder. So the remote url looks something like this:

http://www.myownwebsite.com/

The problem is, I want to use absolute path in the references of all my pages and files. For example:

<link rel="stylesheet" href="/css/style.css">
<script src="/js/main.js"></script>
<a href="/about-us/">About Us</a>
<img src="/images/logo.png" alt="My Website">

Those absolute paths works perfectly on the remote server. But not on my local server, because on my local server, references like /about-us/ links to http://127.0.0.1/about-us/, which cannot find any page.

I want to make absolute paths works on my local server, too, so that I can develop my own website easier. But I cannot simply put all of the files in the root folder on my local server because there are files and folders of other projects.

Is it possible to make local server treats a subfolder (in this case, the folder of my own website) as the root folder?

P.S. The software I'm using for my local server is EasyPHP.


Solution

  • You are required to change VirtualHost and conf_files\httpd.conf file.

    <VirtualHost 127.0.0.1>
    DocumentRoot "${path}/data/localweb/public_html"
    ServerName 127.0.0.1
    <Directory "${path}/data/localweb/public_html">
        Options FollowSymLinks Indexes
        AllowOverride All
        Order deny,allow
        Allow from 127.0.0.1
        Deny from all
        Require all granted
    </Directory>
    

    On windows: configuration->Apache. (opens httpd.conf)

    DocumentRoot "D:/proot"
    (...)
    # DocumentRootDirectory 
    <Directory "D:\proot">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
     </Directory>
     (...)
     NameVirtualHost 127.0.0.1
     <VirtualHost 127.0.0.1>
       DocumentRoot "D:/proot/"
       ServerName localhost
     </VirtualHost>`
    

    Repalce D:/proot/ with your path.