Managing Two Domains on the same Server - server

I have two domains that are both hosted on the same server. Therefore, they both have the same index.html page, and they share all of the other pages. This means that there are two ways to access every file stored on the server:
domain1/file
And
domain2/file
Is there a way to redirect the user to the respective domain1 URL whenever they go to a domain2 URL? The catch is that I only want to redirect if a domain2 URL is gone to.
How can I achieve this programmatically?

Just because you have two domains running on one server does not mean they have to share index.html. The way around this is by using Virtual Hosts. You didn't mention which web server type you are using, so I'll give you an apache example:
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/example2
ServerName www.example.org
# Other directives here
</VirtualHost>
This allows you to have two directories, each serving as a root path for each domain. You'd put the domain1 files in /www/example1, and the domain2 files in /www/example2, in this example. There are some other configuration options you may need, but again depending on your setup, they could vary greatly.
If you are using IIS, there's a writeup over on Server Fault that has information on how to perform that. (This question probably belongs there anyway).

Related

Redirect example.com/path to different server but keep example.com/path

I feel like this is pretty straight forward but struggling to find a clear answer.
All I need to do is point example.com/path to a different server and keep example.com/path as the URL.
My question is: does the receiving server need to have its own domain to receive the redirect?
So, for example I would use a sub domain and point example.com/path -> sub.example.com/path?
How would I do this? Or is there a cleaner way?
We've been advised to keep the path as is (example.com/path) for SEO and not use a sub-domain (although, I suppose it's fine for redirect purposes).
Add this to your main site's Virtual Host configuration:
ProxyRequests Off
ProxyPass /path http://any.other.example.com/path
ProxyPassReverse /path http://any.other.example.com/path
For this you need to have mod_proxy enabled, with this line in your httpd.conf:
LoadModule proxy_module modules/mod_proxy.so
(but I believe it is enabled by default)

Apache virtual hosts with multiple domains

I am trying to figure out what the optimal configuration is for my virtual host files, but I'm having some issues with it.
Currently I have 2 domains: domain1.com and domain2.com
I have 1 server at IP 000.000.000.001 which actually hosts all the needed files. In these files you have an API (api.domain1.com) and the actual website.
domain1.com is using the nameservers of Digital Ocean and uses the following DNS records:
A # 000.000.000.001
CNAME * domain1.com.
For SEO purposes I want to redirect all requests which are not being made to the api subdomain to forward to www.domain1.com.
However, I also ONLY want users to be able to browse my site (and API) through a SSL connection, I don't want users being able to use it through HTTP so I try to redirect all those requests to use HTTPS. The certificates are provided by LetsEncrypt!. Their automated install made changes to my virtual hosts files and currently I have 4 of them:
api.domain1.com.conf
<VirtualHost *:80>
ServerName api.domain1.com
DocumentRoot /var/www/api.domain1.com/web
RewriteEngine on
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,QSA,R=permanent]
</VirtualHost>
api.domain1.com-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName api.domain1.com
DocumentRoot /var/www/api.domain1.com/web
ExpiresActive On
/* ... */
<Directory /var/www/api.domain1.com/web>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLCertificateFile /etc/letsencrypt/live/domain1.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain1.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateChainFile /etc/letsencrypt/live/domain1.com/chain.pem
</VirtualHost>
</IfModule>
domain1.com.conf
<VirtualHost *:80>
ServerName domain1.com
Redirect 301 / https://www.domain1.com
</VirtualHost>
domain1.com-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName www.domain1.com
DocumentRoot /var/www/domain1.com
ExpiresActive On
/* ... */
RewriteEngine On
RewriteCond %{HTTP_HOST} ^https://domain1.com
RewriteRule ^/(.*)$ https://www.domain1.com/$1 [L,R=301]
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ErrorDocument 404 /404.html
SSLCertificateFile /etc/letsencrypt/live/domain1.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain1.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateChainFile /etc/letsencrypt/live/domain1.com/chain.pem
</VirtualHost>
</IfModule>
When I try to visit the site, the following things happen:
http://test.domain1.com --> https://test.domain1.com FAIL
http://domain1.com --> https://www.domain1.com SUCCESS
http://www.domain1.com --> https://www.domain1.com SUCCESS
https://domain1.com --> https://domain1.com FAIL
https://www.domain1.com --> https://www.domain1.com SUCCESS
As you can see, the first and fourth entry fails. They both return a 404 Not Found to my visitors which is unacceptable to me. The first one even shows a SSL warning (I don't think that LetsEncrypt! supports wildcards so I think this warning cannot be prevented?)
What is causing my test. not redirecting to www.?
What is causing my fourth entry to end up serving a 404?
Now I also want my second domain (domain2.com) to point to this server/files. This domain is hosted at a different location (I got it from a friend) and is using different nameservers. I cannot simply change the nameservers (I think?) because this second domain has email hosting linked to it which uses the nameservers of this other provider. Momentarily, this domain also has its own web server (but this will go away in the future) hosted at 000.000.000.002. Right now, it uses a
<meta http-equiv="refresh" content="0; url=https://www.domain1.com" />
tag in its index.html file to redirect to the right server, in the future this will have to be done in the DNS records though.
How should I do this?
So to summarise:
I thought the CNAME * is a catch-all for all subdomains?
What did I do wrong in the virtual hosts file? Something is causing that fourth redirect to fail.
How should I handle my second domain pointing at my first IP?
Is there another way to only allow HTTPS connections? (This should also be forced on the API virtual host). I've heard of HSTS, should I implement this?
Some SEO tests also pointed out that I need a IP redirect to further improve my SEO results. They give this as an example:
RewriteCond %{HTTP_HOST} ^000\.000\.000\.001
RewriteRule (.*) http://www.domain1.com/$1 [R=301,L]
Should I also implement this for SEO purposes? Should I also implement this for my other IP address?
So many questions...
Of course, if you have any other suggestions, please share your opinion!
Lots of questions there! Let's try to work through them:
Not sure what you are asking here, and not an expert in CNAMES but would have thought you'd need '*.domain1.com' at least instead of just '*'
For both you fails you do not explicitly note your alternative server names in either ServerName nor ServerAlias. In this case it will default to the first server found in the config. Is this the api.domain.com one? If so that might explain the 404 as the SERVER_NAME will (by default) be the client supplied one so the api.domain1.com:80 settings will just redirect to the https version of what they sent (which is what appears to be happening), which will then also look at the api.domain1.com:443 config and not find the name. You would need to either add the alternative names to ServerAlias setting of your main domain1.com config, or move that config to be first so it becomes the default and the api one is only used when the api.domain1.com name is explicitly used.
As you appear to want to redirect this straight to your new site I would update the DNS to point to your new IP address, to save you paying for two servers (in terms of money and time to administer). This can be done on the existing DNS nameserver to keep the e-mail though obviously would be nicer to consolidate on one. In the meantime implement a proper 301 redirect in the server config as the meta redirect is not as strong a signal to search engines and you do seem to care about SEO.
The main way is to redirect http to https. HSTS is primarily a security feature which automatically redirects http to https before the browser makes the request. This is useful as the default is http (e.g. typing www.example.com in the browser address bar will, by default, send you to http://www.example.com rather than https://www.example.com), and this can be intercepted to prevent an upgrade to https (which is more difficult to then intercept). It has some downsides though: Not all browsers support it, you need to visit the website at least once to load the HSTS setting (though some browsers allow pre-loading of this), and the setting will expire if the site is not visited in the maxAge time (again assuming it's not preloaded). So it should not be used as a replacement to redirects but more as an enforcement of it for browsers that support HSTS. Blogged about it here if you want to read more on HSTS.
I would also say you have several different methods of redirecting (api:80 has Rewrite, domain1:80 has Redirect, domain1:443 has Rewrite - but with a very restrictive and incorrect RewriteCond). Personally I prefer to use one method as it's less confusing but that's a personal matter (Redirect is also slightly faster but Rewrite gives you more power so I prefer that one). So I tend to have config like this:
<VirtualHost *:80>
#This is the first host so is the default.
#So although I've specified a ServerName and ServerAlias anything else not specified elsewhere will also end up here.
ServerName www.domain1.com
ServerAlias domain1.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
#Redirect everything to https:
RewriteEngine on
RewriteRule ^(.*)$ https://www.domain1.com$1 [R=301,L]
</VirtualHost>
<VirtualHost *:80>
ServerName api.domain1.com
ErrorLog ${APACHE_LOG_DIR}/api.error.log
CustomLog ${APACHE_LOG_DIR}/api.access.log combined
#Redirect everything to https:
RewriteEngine on
RewriteRule ^(.*)$ https://api.domain1.com$1 [R=301,L]
</VirtualHost>
and then for the 443 I would have this:
<VirtualHost *:443>
#This is the first host so is the default.
#So although I've specified a ServerName and ServerAlias anything else not specified elsewhere will also end up here.
ServerName www.domain1.com
ServerAlias domain1.com
ErrorLog ${APACHE_LOG_DIR}/ssl.error.log
CustomLog ${APACHE_LOG_DIR}/ssl.access.log combined
#Redirect everything which is not already on the real www domain name to that:
RewriteEngine on
RewriteCond %{HTTP_HOST} !www.domain1.com
RewriteRule ^(.*)$ https://www.domain1.com$1 [R=301,L]
#all your other 443 config for www.domain1.com
</VirtualHost>
<VirtualHost *:443>
ServerName api.domain1.com
ErrorLog ${APACHE_LOG_DIR}/ssl.api.error.log
CustomLog ${APACHE_LOG_DIR}/ssl.api.access.log combined
#all your other 443 config for api.domain1.com
</VirtualHost>
Note that I've added different log files for each vhost so it's more obvious which vhost is being hit.
Note also that I redirect anything which is not www.domain1.com rather than your version which only redirected if people typed domain1.com (and which I'm not sure would be used as suspect api.domain1.com config came first).
This will also take care of anyone accessing your website by IP address, or any other name as it will always be redirect to your real www.domain1.com server name. This is easy for http requests, but for https requests they may indeed get a cert error if your cert does not include the servername used.
LetsEncrypt do not support wildcards, but they do allow multiple names on a cert so you can easily get a cert for domain1.com and www.domain1.com and you should as this is generally considered best practice. You could also add any other domains you think might be used. For you api.domain1.com domain you could also add to this same cert, or get a separate cert for that since you have separate config for that (your config above is not clear on which you are doing for that since it looks like same config but not sure if that's a typo or not).
For SEO purposes it is indeed best to only serve your site under one URL and use redirects to enforce that URL and above config will do this. This is to prevent Google thinking you have duplicate content if you server the same content under www.domain1.com/page1.html and https://www.domain1.com/page1.html and https://domain1.com/page1.html and http://000.000.000.001/page1.html ...etc.
A lot covered there but hopefully points you in the right direction.

Keep old website running while configuring a new website

I have my website and I want it to be online for customers.
In the mean time I want to upload wordpress to the same domain and start configuring it, but without anyone seeing that.
My website has a simple structure (index.php, about.php, contact.php) and if I upload new WP it will overwrite my old index.php.
Is there anyway I can make it work? Sb goes www.mywebsite.com and is being redirected for example on www.mywebsite.com/old/index.php?
And only from my IP (static) I have normal access to www.mywebsite.com to get the configuration done?
(Assuming your using apache)
Use a new VirtualHost (http://httpd.apache.org/docs/current/vhosts/examples.html)
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example.com
# Other directives here
</VirtualHost>
if you want both to be assiible from the same domain at the same time, you will need either a subdomain or a change of port, Both are doable with the above block of code.
You will need to either reload or restart apache for the changes to take place

Using DNS for redirect naked domain with path

In my DNS provider I set to redirect the naked domain:
http://mydomain.com to http://www.otherdomain.com/index.html
Redirect works, however I need to pass the path also to index.html to whatever means, what could be the solution?
Example:
Pass the path 'abc' to index.html
http://mydomain.com/abc
The abc will be passed into the index.html hosted in otherdomain.com, how can this be done? Or the path during a redirect is lost in translation already?
Update:
As per my understanding DNS providers usually does this through pointing the naked domain into a site where it will do the 301 redirect, where it should still have the path and subfolder in the original request, I'm not really sure.
DNS simply resolves the domain name to an IP. If the domain name requested by the browser is on the web server then the web server will return whichever file is appropriate for the request. An example would be a VHOST in Apache. The DNS server doesn't look up anything relating to the actual files on the URL itself other than the domain name.
To handle this with Apache you might setup the server like this:
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias otherdomain.com
Redirect / http://www.otherdomain.com/
</VirtualHost>

Vhost Zend Framework configuration MAMP - redirected to the index page of the server

I have checked out the posts and made the appropriate changes to the configuration files to make zend framework 2 to work on my local environment. Everything goes fine but the redirection of the page on specifying the vhost name doesnot work appropriately. It displays me the home page of the MAMP server with the directory listing.
Here is what I have done till now:
httpd.conf
<VirtualHost *:80>
ServerName newportalcopper.localhost
DocumentRoot /Applications/MAMP/htdocs/NewPortalCopper/public
SetEnv APPLICATION_ENV "development"
<Directory /Applications/MAMP/htdocs/NewPortalCopper/public>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
etc/hosts
127.0.0.1 newportalcopper.localhost localhost
Can some one tell me what i am doing wrong that this particular thing is not working.
Thanks for viewing the post guys and the help specified. At the end i was able to sort the problem out.
The main issue was the port number in case of MAMP. It was required to be 8888 instead of 80. This specifically solved my problem.