I have a Nuxt.js program that I run on Apache in Ubuntu OS. When I port it to a subdomain like www.example.com, it works fine; But when I port it to the main domain such as example.com, my program does not run. Examples of Ubuntu Server and Apache settings are as follows:
/etc/hostname
example
/etc/hosts
127.0.0.1 localhost
127.0.1.1 ubuntu
100.200.300.40 example.com
100.200.300.40 www.example.com
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
/etc/resolv.conf
nameserver 100.200.300.40
search example.com
/etc/bind/named.conf.options
options {
directory "/var/cache/bind";
dnssec-validation auto;
auth-nxdomain no; # conform to RFC1035
listen-on-v6 { any; };
forwarders {
100.200.300.40;
100.200.300.40;
};
};
/etc/bind/named.conf.local
zone "example.com" IN {
type master;
file "/etc/bind/db.example.com";
};
zone "300.200.100.in-addr.arpa" IN {
type master;
file "/etc/bind/db.100";
};
/etc/bind/db.example.com
$TTL 604800
@ IN SOA ns1.example.com. root.example.com. (
7
7200
900
1209600
86400 )
;
@ IN NS ns1.example.com.
@ IN NS ns2.example.com.
@ IN A 100.200.300.40
@ IN AAAA ::1
ns1 IN A 100.200.300.40
ns2 IN A 100.200.300.40
www IN A 100.200.300.40
/etc/bind/db.100
$TTL 604800
@ IN SOA ns1.example.com. root.example.com. (
6 ; Serial
7200 ; Refresh
900 ; Retry
1209600 ; Expire
86400 ) ; Negative Cache TTL
;
@ IN NS ns1.
@ IN NS ns2.
@ IN PTR ns1.example.com.
@ IN PTR ns2.example.com.
/etc/apache2/apache2.conf
DefaultRuntimeDir ${APACHE_RUN_DIR}
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf
<Directory />
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
AccessFileName .htaccess
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf
/etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
/etc/apache2/sites-available/www.example.com.conf
<VirtualHost www.example.com:80>
ServerName www.example.com
ServerAlias example.com
ServerAdmin webmaster@example.com
ProxyPass / http://localhost:3000/
</VirtualHost>
Now the question is how can I run the program on the main domain and redirect the address for example to www.example.com when someone goes to example.com address?
Update
I already ported my Node.js program to the main domain through the following settings, which did not run:
/etc/apache2/sites-available/example.com.conf
<VirtualHost example.com:80>
ServerName example.com
ServerAdmin webmaster@example.com
ProxyPass / http://localhost:3000/
</VirtualHost>
And I even used the following two code examples to redirect from the main domain to the subdomain, which still did not work:
Redirect permanent / http://www.example.com/
OR
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]