user16421
user16421

Reputation: 307

Redirect all subdomain URLs to matching main domain URLs - redirects with page paths not working

Using Ubuntu 16.04 and Apache virtual hosts.

I have a Wordpress site: www.example.com

Other sites commonly link to URLs such as:

I want to redirect them to:

I have tried:

RewriteCond %{HTTP_HOST} ^sub1.example.com$
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

and

RewriteCond %{HTTP_HOST} ^sub1\.example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

With both of these configurations, I get the following behaviour:

My DNS setup involves A records pointing at the server IP for example.com, www, sub1 etc.

My virtual host .conf file looks like this:

<VirtualHost *:80>
        ServerAdmin [email protected]
        DocumentRoot /var/www/example.com/public_html
    ServerName example.com
    ServerAlias www.example.com

        <Directory /var/www/example.com/public_html/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        <IfModule mod_dir.c>
            DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
        </IfModule>

</VirtualHost>

I have tried adding sub1.example.com as a ServerAlias and reloading Apache to no avail.

How might I solve this?

Upvotes: 0

Views: 332

Answers (1)

user16421
user16421

Reputation: 307

I fixed the issue by creating individual virtual hosts for each subdomain, each with its own files structure and .htaccess file.

Upvotes: 0

Related Questions