Issue
A bit of background: I've been wanting to set up dynamic virtual hosts for a while now. Basically I just wanted to be able to plop a folder into my virtual hosts folder and just have it work without any other configuration. I found out that in chrome any subdomain of .localhost
will behave the same as localhost
. This means that I can use .localhost
as a TLD for all my projects and I don't have to edit my HOSTS
file for every new virtual host I want to add.
I read the documentation on https://httpd.apache.org/docs/current/vhosts/mass.html and figured out how to have dynamic virtual hosts based on the host header.
After reading through that page and other resources on the internet, I came up with the following configuration in my httpd-vhosts.conf
file. This uses the part before the .localhost
to determine the folder name.
<VirtualHost *:80>
ServerAdmin admin@localhost
# Get the server name from the Host header
UseCanonicalName Off
# Log
LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
CustomLog logs/vhost_access.log vcommon
ErrorLog logs/vhost_error.log
# Match domain name against a folder
VirtualDocumentRoot "C:/vhosts/%-2+"
<Directory "C:/vhosts/*">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
So with this set-up, I can create a new folder in the vhosts
folder that contains all my html documents. So, for instance, if I create a folder named project1
I can access that folder by going to http://project1.localhost
.
Now my main problem is that I don't always need to create a new virtual host. I just want to create a random php file and access it by going to http://localhost/index.php
. However, with the above configuration, just using http://localhost
caused an error. Probably because the pattern in the VirtualDocumentRoot
directive is using the subdomain of localhost
and there is no subdomain when I just use http://localhost
.
tl;dr:
Given the configuration above, is there any way to have a hard-coded virtual host for localhost
and have dynamic virtual hosts for subdomains of localhost
?
Or, how can I create a configuration that would allow me to have this:
http://localhost -----------------> C:/vhosts/
http://project1.localhost --------> C:/vhosts/project1
http://project2.localhost --------> C:/vhosts/project2
http://blog.project2.localhost ---> C:/vhosts/project2/blog
Solution
I have a solution but it just solve part of your problems.
<VirtualHost *:80>
ServerAdmin admin@localhost
ServerName localhost
DocumentRoot "C:/vhosts/"
</VirtualHost>
<VirtualHost *:80>
ServerAdmin admin@localhost
ServerName longnameyoullneveruse.blabla.localhost
ServerAlias *.localhost
DocumentRoot "C:/vhosts/"
RewriteEngine On
RewriteMap lowercase int:tolower
RewriteCond %{HTTP_HOST} ^(.*)\.localhost$
RewriteRule ^(.*)$ "C:/vhosts/${lowercase:%1}/$1"
# you should use all lowercase for subfolders' name
</VirtualHost>
This should meets the first three lines of your requirement:
http://localhost -----------------> C:/vhosts/
http://project1.localhost --------> C:/vhosts/project1
http://project2.localhost --------> C:/vhosts/project2
Answered By - Shiji.J Answer Checked By - Clifford M. (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.