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

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?

Related

Apache multihost does not respect server name

I have two django applications on one machine. I would like to run them on the same address and port. So the user should be point to the correct address according the request domain/host. My current apache.conf file looks like:
<VirtualHost *:80>
ServerName 35.232.14.xxx
...
</VirtualHost>
<VirtualHost *:80>
ServerName www.my_testing_domain.com
...
</VirtualHost>
Note that there is real IP address and real domain and other stuff required by Django app. I build the file according to the documentation: https://httpd.apache.org/docs/2.4/vhosts/examples.html#page-header (apache multiple sites on one IP) and https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04 (django with apache).
The thing is, that every request goes to the first VirtualHost (in this case the IP address). If I switch their order in the apache.conf, then every request goes to the domain VirtualHost.
So one of the sites is working correctly (always the first one).
Why the apache does not respect the server names? What am I missing?
I add Server alias withou "www":
ServerAlias my_testing_domain.com
to the "domain based" virtual host. Now it works.

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.

Apache Non-Vhost Redirect

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.

IP of Web server stays in URL instead of showing Domain Name

So lets say I have the Domain Name "www.example.com" and have a "http-redirect" to server "1.1.1.1" where apache2 is running on Ubuntu 14.04 Server amd64.
I have all my web files on the server and they are working perfect.
What is not working right is the URL. Instead of "www.example.com/sites.php" if have "1.1.1.1/sites.php". How can I fix this?
What I did so far:
Disabled the default Virtual Host and created a new one:
<VirtualHost *:80>
ServerAdmin bla
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www
ErrorLog ...
CustomLog ...
</VirtualHost>
(It's typed, since I can't copy from my console..)
I tried to enter in the /etc/hosts
1.1.1.1 www.example.com
Thanks for any help
As discussed in comments, the reason that you see IP address in your browser is that http-redirect is pointing directly to to IP address, there's no domain name to display. Redirect just tells browser to go elsewhere and browser then fetches website from supplied URL, same as if you'd click on a link. If you redirect URL to 1.1.1.1/sites.php than that is what you will see in browser.

limit apache virtualhost to one domain?

I have configuration of virtualhost:
<VirtualHost xx.xx.xx.xxx>
ServerName mydomain.com
ServerAlias *
...
</VirtualHost>
everything works fine on mydomain.com. But the problem is that someone linked his domain to my application ip and I want to avoid this. Is it possible to limit virtualhost to only one domain "mydomain.com" - so if somebody will point his domain to my ip - virtualhost will not process this request?
There's no way to prevent it from answering HTTP requests on that IP address. I think that if you were to use name based hosting and then create a vhost with ServerName 1.2.3.4 then it'd be matched for requests to the IP address and you'd be able to add some special behaviour there (return an error code, redirect to the named site, etc).
If you just want to make sure that people don't bookmark the IP address and always see mydomain.com in the browser then have a look at the section on Canonical Hostnames in http://httpd.apache.org/docs/2.0/misc/rewriteguide.html