How to create Virtual Host from Apache .htaccess? - apache

I am using Apache Friends XAMPP in Windows (Local Server). I setup the virtual host in httpd-vhosts.conf in an Apache configuration directory like this
NameVirtualHost *:80
<VirtualHost *:80>
ServerName test.example.com
DocumentRoot "E:\xampp\htdocs\example"
</VirtualHost>
This works fine when I browse the URL
http://test.example.com
Is it possible to create virtual host from Apache .htaccess dynamically?

The context for VirtualHost has to be server config. See the Apache docs.
This means that the directive may be
used in the server configuration files
(e.g., httpd.conf), but not within any
or
containers. It is not allowed in
.htaccess files at all.
(Directive Dictionary)

It seems to be impossible. Because your .htaccess is used only after your host is resolved by root configuration files of a server like httpd.conf, apache2.conf.
To put simply, .htaccess in the www directory or in its subdirectories will only be processed after root configuration files are processed.
I mean you type http://host.name and apache finds the destination and uses .htaccess file on the host to perform some operations if needed.

Related

Allow Cachet to run with other sites

I have installed the open source status page Cachet on my macbook's local web server and it works perfectly but during the set up it tells you to change your apache's virtual host to route all traffic to Cachet. I am trying to allow Cachet to run by going to the main domain but if I go to domain/test I would like it to go to another web page. I tried adding another vhost like this:
<VirtualHost *:80>
ServerName http://domain/test
DocumentRoot /Users/macbook/Sites/
</VirtualHost>
but this does not work, I just get error 404 when trying to reach /test page.
Cachet is built using the Laravel framework, so there is a front controller in the public/ directory. That's why you need to route all the requests to this file. But you do not need to route all the traffic of your server to Cachet, that's just the traffic of the site (Cachet), in order to /setup and others are executed by /public/index.php.
Your vhost file may be simple, below an example of what it could be.
It is a simple vhost configured in Mamp, on OSX.
<VirtualHost *:80>
DocumentRoot "/Applications/MAMP/htdocs/Cachet/public"
ServerName status.mysite.com
<Directory /Applications/MAMP/htdocs/Cachet/public>
AllowOverride All
</Directory>
</VirtualHost>
The routing to the public/index.php file is performed by a .htaccess file, so you don't have to write this configuration in your vhost. All you need to do in your vhost is to allow the .htaccess execution.
Note for local web server
If you want to use a custom domain name, you'll need to
update the /etc/hosts file to match the domain with your loopback IP
address.

Additional virtualhost with Websphere apache plugin

Current I have Apache Httpd and Websphere with Apache config generated with Websphere apache plugin. What I want to do is write virtualhost which overlap some of move with usage of provy pass, for example:
<VirtualHost *:80>
ServerAlias test-*-test
ServerSignature On
ProxyPreserveHost On
ProxyPass /test/ ajp://localhost:1234/test/
</VirtualHost>
Earlier in this conf file I have only WebSpherePluginConfig with path.
New virtualhost is working as it should, but unluckily it overrides somehow all plugin config (even for another server aliases). How to make it to only override this specific path.
The first listed virtual host for *:80 is the default. Add a new first vhost without proxypass, so the vhost in your question will only be used when the hostname actually matches.

Setting up virtual host on XAMPP

I've installed XAMPP on Ubuntu in '/opt/lampp' directory and would like to set up some VirtualHosts. Apache Virtual Host tutorial instructs to place
<VirtualHost *:80>
...
</VirtualHost>
code in '/etc/apache2/sites-available/[virtualhostname].conf'. The problem is that I don't have 'apache2' folder in '/etc'. I also don't have 'sites-available' directory in '/opt/lampp/apache2'.
I have '/opt/lampp/etc/httpd.conf' and '/opt/lampp/etc/extra/httpd-vhosts.conf' files though. Which ones shall I use to set the VH up?
You should use /opt/lampp/etc/extra/httpd-vhosts.conf, but before you should add your servername to your .hosts file and uncomment inclusion of of the httpd-vhosts.conf in the /opt/lampp/etc/httpd.conf.

Apache named-based VirtualHost configuration for multiple sites in one host

My problem is simple and i think the solution also.
I search the forums about 2-3 hours and didnt my answer... -_-' .
I got "space" in a host provider. So they gave me a "www" directory.
I have multiple folders in this directory so it's like:
www--
|
/Folder1
/Folder2
/Folder3 etc.etc.....
Each folder represents a website.
So i want to redirect each website to each folder e.g. :
www.example1.com -> Folder1
www.example2.com -> Folder2
www.example3.com -> Folder3 etc....
-BUT- without showing the subfolder of each ...
So this is NOT a solution: www.example1.com/Folder1
This IS a solution: www.example1.com .
How can i modify my .htaccess file in root WWW ?
Thanks in advance !
This has to be done through your http server configuration (like apache).
VirtualHosts is how you split multiple domains across one computer, and their respective .htaccess configuration will give you protection for your site (username/password).
If you just want to split the computer into multiple hosts, you don't need to use .htaccess.
See the following links
Here are some VirtualHost Configuration Examples
Apache HTTP Server Tutorial
Server Config Files
Excerpt from the "example's" link on "Name-Based Virtual Hosts":
<VirtualHost 172.20.30.50>
DocumentRoot /www/example1
ServerName www.example.com
# Other directives here ...
</VirtualHost>
<VirtualHost 172.20.30.50>
DocumentRoot /www/example2
ServerName www.example.org
# Other directives here ...
</VirtualHost>
The configuration file is usually located in /etc/apache2/apache2.conf which then reads from /etc/apache2/sites-enabled/*.conf which are essentially symbolic links to /etc/apache2/sites-available/*.conf - those files are used with the above code to accomplish the results you describe.
You may also be interested looking into nginx, an alternative to apache2.
.htaccess is not what you're looking for. Instead, read up on Name-based Virtual Host Support. You will need to add some entries to your httpd.conf file to point each domain to the correct folder in the www directory. It should be pretty straight forward.
http://httpd.apache.org/docs/2.2/vhosts/name-based.html

setting a default apache virtual host

Is there any better way of setting the default apache virtual host other than it just picking the first config it finds?
I have a server with many domains, of which only some are configured with httpd but the default virtual host severed up is for example is aaa.com where as really I would like it to default to mmm.com instead?
Something like parking domains without going through the hassle of setting up a config for each one - then I can serve a "content this domain has not been created yet" page?
Cheers
You can create a default virtual host and name it something like 000-default so that it loads first and is used unless another vhost matching the requested domain is found. Here's the bare-bones 000-default:
<VirtualHost *:80>
DocumentRoot /var/www
<Directory /var/www >
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Then you can setup a PHP file under /var/www to create a domain parking page (this is a very simplified example):
<?php
printf('The domain <b>%s</b> is being parked',
htmlentities($_SERVER['HTTP_HOST']));
?>
The first sites-available conf file is default (alphabetically). There should be a 000-default.conf file already, if not create it.
Edit this one to your liking and then make sure it's enabled a2ensite 000-default.conf. And apache2 is reloaded sudo service apache2 reload.
Then any request that isn't caught by your other vhosts will come here.
Use ServerAlias in a name-based VirtualHost, you will only have to add one line per each new domain.