Apache Non-Vhost Redirect - apache

I want to force all server requests not matching one of my configured vhosts to redirect to my company's home page?
Currently my primary DocumentRoot is set to my home site directory, so non-vhost request do serve home page content; however, the domain name does not change. How can I force this?
Also, my primary ServerName is commented out by default. Is setting this recommended? And if so, why?

The first virtual host found in the apache config will be the default host if there is no ServerName match. So add a default host first that redirects to your companies home page:
<VirtualHost *:80>
ServerName myserver
Redirect 301 / https://www.example.com/
</VirtualHost>
If you have more than one IP address and different listeners x.x.x.x:80 then it can be a bit more complex as apache actually looks for a match on the combination of listener and servername. But it's not hard to resolve.
If the primary servername is commented out then apache will try to figure this out at start up. It's usually not an issue when using VirtualHost directives with ServerName local to each vhost.

Related

access to a server apache that has virtualhost

I have a cloud server with lampp installed on. I had configured a virtual host here like that:
<VirtualHost xx.xxx.xx.xxx:80>
DocumentRoot "/opt/lampp/htdocs/folder/"
ServerName www.xxx.com
</VirtualHost>
and everything work as i expect, if i go to www.xxx.com i see my 'folder' site.
Now i need to work to another site present on the same server, but it doesn't allready have a domain, so i had imagine (by reading the apache's configuration file explanation)that i have to do it in this way:
<VirtualHost xx.xxx.xx.xxx:80>
DocumentRoot "/opt/lampp/htdocs/folder/"
ServerName www.xxx.com
</VirtualHost>
<VirtualHost xx.xxx.xx.xxx:80/test>
DocumentRoot "/opt/lampp/htdocs/test/"
</VirtualHost>
But it doesn't work, if i do http://xx.xxx.xx.xxx:80 i reach the 'folder' site while if i do http://xx.xxx.xx.xxx:80/test rather the reach the 'test' site i still reach www.xxx.com, why? How could i reach this objective?
The virtual host defined first (top most) acts as default host. That one is used to respond to any incoming requests that are not matched by a specific host name in the request.
You want to try this setup:
# some fallback host for testing and development
<VirtualHost xx.xxx.xx.xxx:80>
DocumentRoot "/opt/lampp/htdocs/_default"
</VirtualHost>
# a virtual host with a specific host name
<VirtualHost xx.xxx.xx.xxx:80>
DocumentRoot "/opt/lampp/htdocs/example.com"
ServerName example.com
ServerAlias www.example.com
</VirtualHost>
(here xx.xxx.xx.xxx stands for the systems public and routable IPV4 address)
In your file system you have this hierarchy:
/opt/lampp/htdocs/
_default/
test1/
test2/
example.com/
This way requests to http://example.com or http://www.example.com are mapped to the folder /opt/lampp/htdocs/example.com, requests to URLs with any other host name to the default folder /opt/lampp/htdocs/_default in which you now can create as many sub folders as you like for different applications.
Another approach would be to use other host names below your existing domain name for internal tests, so something like test1.example.com or similar. That way you do not have to use raw IP addresses with their routing risk.

mediawiki instance served by apache on specific domain

So, I use MediaWiki, hosted by Apache (wiki dir is in /var/www/html/wiki).
I have my domain www.my-domain.com which is redirected through apache virtual host to port 8080 for some nodejs app.
I have now tried to set up my wiki to be served on http://wiki.my-domain.com
In my DNS records i have both www and wiki domains redirected to same server.
First I tried to make apache virtual host to handle domain request:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName my-domain.com
ServerAlias wiki.my-domain.com
DocumentRoot /var/www/html/wiki
</VirtualHost>
However if I tried to connect to wiki.my-domain.com or wiki.my-domain.com/wiki I got the message that the page doesn't exists.
I went to check my wiki's LocalSetting.php
and I tried editing this line:
## If i changed value to this, it didn't worked
$wgServer = "http://wiki.my-domain.com";
## If i set value to this, it was working
$wgServer = "http://my-server-IP";
However, if i set wgServer to my server IP, whenever i write http://wiki.my-domain.com, i get redirected to http://my-IP/wiki instead of staying on domain name
I would appreciate the help on how to properly set up my wiki's or apache's settings to properly host my wiki on my domain.
I found the problem:
$wgScript was set to:
$wgScript="/wiki"
and needs to be set to:
$wgScript=""
since Apache was redirecting the domain already to my /wiki folder, there was no need for script to also redirect it there and this was causing problems.

How to make a Permanent redirect to an internal ip address with apache in Ubuntu?

I have the following problem: I want to redirect any request received on the external ip lets say 192.168.x.x, to an internal ip of the same machine, lets say 172.0.0.1. The internal ip has been associated to a name editing the hosts file as follows:
$/etc/hosts
...
...
172.0.0.1 www.example.com
I want to have www.example.com in the HTTP_HOST field of the request header unregarding to which external ip has been requested (In order to have django sites working properly without adding the current ip every time to the site list). Consequently I tried the following:
$/etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:443>
ServerName entrypoint.com
Redirect 301 / https://www.example.com:443
</VirtualHost>
<VirtualHost 172.0.0.1:443>
ServerName www.example.com
...
...
</VirtualHost>
When I try to connect from a browser the redirect seems to look for a web domain instead of the local ip. How can I fix this?

Apache different sites on different ports, still links to same site. Bind9 for domain names

I've been trying to create 3 different domains linking to 3 different sites on the same machine, 2 which works but the third on the different port links to the first page.
My apache config looks like this:
Listen 81
NameVirtualHost *:81
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/www
</VirtualHost>
<VirtualHost *:80>
ServerName www2.example.com
DocumentRoot /var/www/www2
</VirtualHost>
<VirtualHost *:81>
ServerName controlpanel.example.com
DocumentRoot /var/www/controlpanel
</VirtualHost>
I've used Bind9 to set up the domains.
www IN A 123.123.123.123
www2 IN A 123.123.123.123
controlpanel IN A 123.123.123.123
www and www2 works fine and shows the correct site, however controlpanel.example.com also links to the first www site. When I enter the port manualy on the ip, xxx.xxx.xxx.xxx:81, i get linked correctly. The thing is that I don't really know where I'm doing it wrong, this is the first time I'm trying anything like this. You got any ideas?
Im also running this on an old ubuntu 12.04 server.
Regarding where you're going in the comments for the previous answer:
You could add a port 80 virtualhost for controlpanel.example.com and put a single statement inside,
Redirect / http://controlpanel.example.com:81
The purpose of the ServerName is not to inform the browser what port your webserver is using. It's used for name-based virtualhosts and as a last resort for self-referential links (out of the box, self-referential links are generated with whatever the client already thought it was accessing via the Host: header)
But there is definitely something quite bizarre about your requirement. Usually the motivation is to not use custom ports, and if they are, to address the server with a low port and have the por remapped by some intermediary (load balancer, proxy).
If you want your third virtualhost to be simulataneously the defautl on port 81 and a name-based option on port 80:
Change
<VirtualHost *:81>
to
<VirtualHost *:80 *:81>
Apache finds the set of virtual hosts with the best IP:PORT based match first, then if NameVirtualHost also matches, starts looking at the ServerNames from that set.

Set up host file using port

I want to setup my host file to
127.0.0.2:5050 domain2.com => this is a local domain
when a type in my browser domain2.com, this return me : HTTP Error 404. The requested resource is not found.
i use this in apache
<VirtualHost 127.0.0.9:5050>
ServerAdmin info#domain2.com
DocumentRoot "C:/Users/My_Dir/LOOP/WebEnginer-2011/domain2_Dir/"
ServerName domain2.com
DirectoryIndex index.php index.html index.htm
ServerAlias www.domain2.com
ErrorLog "c:/wamp/xxxx/xxxx.log"
CustomLog "c:/wamp/xxxx/xxxx.log" common
</VirtualHost>
<VirtualHost 127.0.0.9:5050>
ServerAdmin info#domain2.com
DocumentRoot "C:/Users/My_Dir/LOOP/WebEnginer-2011/domain2_Dir/admin_Dir/"
ServerName admin.domain2.com
DirectoryIndex index.php index.html index.htm
ServerAlias www.admin.domain2.com
ErrorLog "c:/wamp/xxxx/xxxx.log"
CustomLog "c:/wamp/xxxx/xxxx.log" common
</VirtualHost>
but when i type 127.0.0.2:5050 i can see a web page. I want to use subdomain like admin.domain2.com
i can't use port 80 because IIS use that port.
How can i set up my host file to listen domain2.com?
That won't work since the hosts file only serves the purpose of mapping a hostname to an IP-address. The port number of a service is a different concept and is not handled by the "hosts" file nor the DNS-System. In Short: you can't supply a port number in the "hosts" file.
If your Webserver works on another port, you have to supply that information in the URL: http://domain2.com:5050.
The only other solution is to configure your Webservers to listen on a specific IP so that they don't interfere with each other. For example the IIS could listen on 127.0.0.1 and the Apache on 127.0.0.2 (the way you have already configured it).
There's a HOWTO for achieving that with the IIS. I'm not sure if that works for 127.0.0.x-IP's but I think it's worth a try.
It might be:
Your DNS resolver not resolving that properly
Some Apache webserver misconfiguration
Try this to get more information about that:
What if you ping domain2.com?
Also, try what happens if you put something like domain2.local in your hosts file. It might be some windows security c** disallowing you to overwrite the ip of an existing domain.
Why didn't you use 127.0.0.1? That should be fine, however
Make sure you have a properly configured VirtualHost that accepts requests to "domain2.com", or you just have a default virtualhost.
EDIT
What did you actually add to hosts file? The correct syntax would be:
127.0.0.2 domain2.com