Harts
Harts

Reputation: 4093

rewrite rule for any subdomain to main domain

I have a site called mailcake.com, and I would like to have beta.mailcake.com, to redirect to mailcake.com, How can I achieve this? I have it like this on /etc/httpd/conf.d/mailcake.conf

NameVirtualHost *:443

<VirtualHost *:80>
    DocumentRoot /var/www/mailcake
    ServerName mailcake.com
    ServerAlias www.mailcake.com beta.mailcake.com

RewriteEngine On
RewriteCond %{HTTP_HOST} ^beta\.mailcake\.com$ [NC]
RewriteRule (.+)$ "http://mailcake.com" [L,P]   

    ErrorLog /var/log/mailcake.com-error_log
    <Directory />
        Options Indexes FollowSymLinks
        AllowOverride All
    </Directory>
</VirtualHost>

<VirtualHost *:443>
    DocumentRoot /var/www/mailcake
    ServerName mailcake.com
    ServerAlias www.mailcake.com

SSLEngine on
SSLCertificateFile /var/www/mailcake.crt
SSLCertificateKeyFile /var/www/mailcake.key
    ErrorLog /var/log/mailcake.com-error_log
    <Directory />
        Options Indexes FollowSymLinks
        AllowOverride All
    </Directory>
</VirtualHost>

but it does not work

Upvotes: 1

Views: 1627

Answers (1)

icabod
icabod

Reputation: 7074

The following seems to work when tested with a few URLs:

RewriteCond %{HTTP_HOST} ^(beta\.)?mailcake\.com
RewriteRule (.*) http://mailcake.com/$1 [QSA]

Part of the important bit here is adding the $1 to ensure that any paths get added to the URL (in your example, http://beta.mailcake.com/stats/ would get redirected to http://mailcake.com/, which possibly isn't desireable. Also, adding [QSA] should ensure any query strings are passed along too.

If you're doing the redirect within the httpd.conf, I probably also wouldn't add [L], as it could stop other valid redirects within any .htaccess files from working.

Upvotes: 1

Related Questions