Forum Moderators: phranque

Message Too Old, No Replies

Redirect *.domain.tld to www.domain.tld

via ServerAlias or ModRewrite

         

root666

12:22 pm on Mar 8, 2010 (gmt 0)

10+ Year Member



Hello

I have the following records in the DNS

domain.tld A xyz.xyz.xyz.xyz
www CNAME domain.tld
* CNAME domain.tld

The website is built on wordpress. The VirtualHost settings are:

<VirtualHost *:80>
ServerAdmin support@domain.tld
DocumentRoot /var/web/domain.tld
ServerName www.domain.tld
ServerAlias *.domain.tld domain.tld
CheckSpelling on
<Directory />
AllowOverride all
</Directory>
ErrorLog /var/log/apache2/domain.tld.error
CustomLog /var/log/apache2/domain.tld.access common env=!dontlog
</VirtualHost>

When I write domain.tld in the address bar I am redirected to www.domain.tld. But when i wtite test.domain.tld I am not being redirected and the url stays test.domain.tld . How can I achive to be redirected to www.domain.tld when typing any subdomain of domain.tld? Obviously the VirtualHost directive does not work (using apache2). How can I do it with modRewrite? Now the contents of htaccess are:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Please post some feedback, deeply appreciated.

g1smd

7:42 pm on Mar 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There's been two Wordpress threads in the last week, both with significant code improvements listed and discussed.

One of them also answers your exact question and more.

root666

7:20 am on Mar 10, 2010 (gmt 0)

10+ Year Member



Unfortunately the code from the two other threads did not worked for me. Any other ideas?

jdMorgan

7:55 pm on Mar 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, you need a "domain or hostname canonicalization" redirect, best implemented using mod_rewrite. There are several valid forms to choose from, and each has been posted hundreds of times here -- Try the WebmasterWorld site search feature to find them, and the accompanying discussion of which method is best for each application.

Jim