Saturday, May 4, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 355  / 2 Years ago, thu, may 12, 2022, 10:14:15

I need to have multiple local sites in apache. So I add a virtual host for each one:



#/etc/apache2/sites-available/example.com:
<VirtualHost 127.0.0.1:80>
ServerAdmin [email protected]
ServerName www.example.com
ServerAlias example.com

# Indexes + Directory Root.
DirectoryIndex index.php
DocumentRoot /var/www/example.com/htdocs/

# CGI Directory
ScriptAlias /cgi-bin/ /var/www/example.com/cgi-bin/
<Location /cgi-bin>
Options +ExecCGI
</Location>


# Logfiles
ErrorLog /var/www/example.com/logs/error.log
CustomLog /var/www/example.com/logs/access.log combined
</VirtualHost>


And then I do sudo a2ensite example.com and sudo service apache2 restart.
The problem is that root-relative paths don't work. For example when I say <link href="/css/somefile.css" rel="stylesheet"> somewhere in /var/www/example.com/htdocs/index.php, apache fails to serve /var/www/example.com/htdocs/css/somefile.css to browser (browsing: localhost/example.com/htdocs)



What I'm missing?


More From » apache2

 Answers
4

I think the problem is that you access the site with localhost/example.com/htdocs



You can do the following:



In your vhost configuration switch ServerName and ServerAlias:



ServerName  example.com
ServerAlias www.example.com


restart apache2 with:



sudo /etc/init.d/apache2 restart


Now in /etc/hosts add the 2 lines:



127.0.0.1 example.com
127.0.0.1 www.example.com


You sould now be able to access the site with www.example.com or example.com.
(This works if you are working on the same machine where you have your apache installed)



And your path should work as expected.



Please comment, if you have any problems.


[#28126] Saturday, May 14, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cocal

Total Points: 236
Total Questions: 111
Total Answers: 123

Location: Belarus
Member since Tue, Mar 14, 2023
1 Year ago
;