'Apache redirect all traffic to another domain preservign links

I have an apache web server serving a wordpress site on an AWS instance with a route53-owned domain. Let's say the domain is utopia.com. I'd like for all requests (both the subdomains and pages like utopia.com/products) to be redirected to a new domain which my friend owns: utopia.co. So whenever the user goes to utopia.com/products the content they would see would be served by utopia.co/products and so on.

What is correct httpd rule for redirecting all traffic to another domain this way? I tried 301.. didn't seem to do the trick.


I have two configs for secured and unsecured trafic: httpd-le-ssl.conf:

<IfModule mod_ssl.c>
<VirtualHost *:443>

    DocumentRoot "/var/www/html"
    ServerName "utopia.com"
    Redirect 301 / https://www.utopia.co/ #<------------- Doesn't seem to work :(
    ServerAlias "www.utopia.com"

Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/utopia.com-0001/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/utopia.com-0001/privkey.pem
</VirtualHost>
</IfModule>

httpd.conf:

ServerRoot "/etc/httpd"
Listen 80

<VirtualHost *:80>

    DocumentRoot "/var/www/html"
    ServerName "utopia.com"
    Redirect 301 / http://www.utopia.co #<---------- Doesn't work :(
    ServerAlias "www.utopia.com"

RewriteEngine on
RewriteCond %{SERVER_NAME} =utopia.com [OR]
RewriteCond %{SERVER_NAME} =www.utopia.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost


<Directory />
    AllowOverride none
    Require all denied
</Directory>

DocumentRoot "/var/www/html"

<Directory "/var/www">
    AllowOverride None
    Require all granted
</Directory>

<Directory "/var/www/html">
    Options Indexes FollowSymLinks

    AllowOverride All
    Require all granted
</Directory>

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

<Files ".ht*">
    Require all denied
</Files>

ErrorLog "logs/error_log"

LogLevel warn

<IfModule log_config_module>

    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>

    CustomLog "logs/access_log" combined
</IfModule>

<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

</IfModule>

<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>

<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz

    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>

AddDefaultCharset UTF-8

<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>

#
# Customizable error responses come in three flavors:
# 1) plain text 2) local redirects 3) external redirects
#
# Some examples:
#ErrorDocument 500 "The server made a boo boo."
#ErrorDocument 404 /missing.html
#ErrorDocument 404 "/cgi-bin/missing_handler.pl"
#ErrorDocument 402 http://www.example.com/subscription_info.html
#
EnableSendfile on

<IfModule mod_http2.c>
    Protocols h2 h2c http/1.1
</IfModule>


IncludeOptional conf.d/*.conf
Include /etc/httpd/conf.d/aa_utopia-ssl.conf


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source