The solution -
Step 1: Create a sub-directory inside /var/www/html and place an index.php there
sudo mkdir /var/www/html/mysecondary
sudo cd /var/www/html/mysecondary
sudo nano index.php
Add following lines in index.php file
<?php
phpinfo()
?>
Step 2: Copy 000-default.conf file and modify its content
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mysecondary.com.conf
sudo nano /etc/apache2/sites-available/mysecondary.com.conf
Modify following lines
ServerName myprimary.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
<Directory /var/www/html/>
AllowOverride All
</Directory>
to the following
ServerName mysecondary.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/mysecondary
<Directory /var/www/html/mysecondary/>
AllowOverride All
</Directory>
Save the file.
Step 3: Tell apache to reconfigure
sudo a2ensite /etc/apache2/sites-available/mysecondary.com.conf
and restart apache
service apache2 reload
This will append the secondary site information to the default configuration with the primary site
Now if you have correctly set the A record of the secondary domain to your Droplet IP, the domain will be reachable and show the phpinfo() content in the browser. But the same content will be available on myprimary.com/mysecondary link to. Step 4 solves this problem.
Step 4: Open or create .htaccess file in your primary domain root (ie /var/www/html)
sudo nano /var/www/html/.htaccess
And the add the following lines:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^myprimary\.com [NC]
RewriteRule ^apples/(.*) http://mysecondary.com/$1 [R=301,NC,L]
Do not add the first two lines if there are already there.
Now “myprimary.com/mysecondary” will be permanently redirected to “mysecondary.com”
And both the sites are working independently now, add scripts for the secondary domain into the mysecondary sub-directory
Caution: Create a backup of .htaccess file before modifying it, if you run into some trouble just restore it. You may have to clear cache in your browser (better open an incognito window) to access the secondary domain.
Dear @ask .
Thanks for this instruction.
I followed it but run into a couple of glitches. (please note that I am a complete newbie).
I did Step 1 but the command line said that CD is not found.
So what I did, i went via FTP and created the index file.
For Step 2, I also made do with FTP by copying the conf file and editing it.
Step 3> sudo a2ensite from command line said that website.com.ph.conf is not existing, so i tried to do it on the command line >step 2.
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mysecondary.com.conf
sudo nano /etc/apache2/sites-available/mysecondary.com.conf
it seems that it did the files, but when i redid step 3, i still get the same error. on the ftp, i can see the file is there. i did try to restart apache.
apache is restarted fine.
did the htaccess.
now, the site is redirecting to main site.
(ps, i have already added the domain at digital ocean networking tab.)
may i please have a comment on what i can do further to fix it?
thank you in advance!
Ps.
i did the a2ensite command again and it said:
root@xx:~# a2ensite website.com.ph.conf
Site website.com.ph already enabled
was able to fix my problem.
this helped:
https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts
Hi @april,
I think your web root directory is /var/www/ (not /var/www/html), that may be the reason for the Step 1 error
Further, if you have created the subfolder (where you’ll store the files of second site) in your /var/www/ directory , it is fine.
Let assume your subfolder location is /var/www/mysecondary
And your second domain is mysecondary.com (it may be any domain you have purchased and DNS set to your digitalocean droplet IP)
Open the following file
Add/edit to these line
Save the file by Ctrl + x
Please NOTE: Replace mysecondary.com with your second domain name and /var/www/mysecondary with the subfolder you have created for storing the files of your second file.
Now tell apache with reconfigure
Please NOTE: Replace mysecondary.com with your second domain name. New Apache server does NOT require full path of the file
Then restart the server
Next, do the .htaccess modification as mentioned in the original answer.
I think it was my conf file that was the trouble.
This helped me solve the problem.
https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts
specifically the server alias and the document root. plus my server name was #commented. :P this might be because i just copied the original 000 conf instead of doing it via the command line as you instructed.
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/publichtml
ErrorLog ${APACHELOGDIR}/error.log
CustomLog ${APACHELOG_DIR}/access.log combined
</VirtualHost>
Thank you again. Without this, i would never have found the courage to do it.