Forum Moderators: phranque

Message Too Old, No Replies

Redirect non existing subdomain to domain

         

Badgroovy

12:02 am on Jul 9, 2006 (gmt 0)

10+ Year Member



Hi, i want to redirect a NON EXISTING subdomain to my domain, i.e.

If i have <anysubdomain>.mywebpage.com, i want it to redirect to:

www.mywebpage.com/index.php?subdomainTyped=<anysubdomain>

The means of this, is that i want to know what subdomains people type in my domain.

I have tried using ErrorDocument 404,

but it only works if i type somehitng like this: www.mywebpage.com/<anysubdomain>

I have also tried somehitn like this, and still not working:

RewriteEngine On
RewriteCond %{HTTP_HOST}!www.mywebpage.com$
RewriteCond %{HTTP_HOST}!^my_ip_adress
RewriteCond %{HTTP_HOST} (www\.)?([^.]+\.)(.*)$
RewriteRule ^(.*)$ [%3...] [L]

I also did this, i used my cpanel to creato a subdomain and redirect it to my main index page, and copied the .htaccess created for that subdomain, this is what my cpale created:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysubdomian.mywebpage.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.mysubdomian.mywebpage.com$
RewriteRule ^(.*)$ [mywebpage.com...] [R=301,L]

I tried changing mysubdomian for (.*), but the result was my domain not working.

Any ideas?

Thks in advance...

hedon

7:16 am on Jul 10, 2006 (gmt 0)

10+ Year Member



for this king of subjects you have to do more than a redirect rule with mod_rewrite/htaccess (I've bin arguing for a month with my hosting company about smth like this).

- Fisrt you have to edit the DNS zone of the website, adding an A that shall have as domain this "*" (It would be best if you add another A with "www.*" aslo).

- After this you should modify you httpd.conf, adding a new virtual host looking like this (what fallows is just an example):

# This is the domain:
<VirtualHost *>
ServerAdmin webmaster@localhost
DocumentRoot /var/www
ErrorLog /var/log/apache2/error.log
ServerName domain.com
</VirtualHost>

# This is what you add:
<VirtualHost *>
ServerAdmin webmaster@localhost
DocumentRoot /var/www
ErrorLog /var/log/apache2/error.log
ServerName *.domain.com
</VirtualHost>

- After this two things there comes the last part: .htaccess
What you CPanel gave you is very good with one little possible change:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.mywebpage\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.(.*)\.mywebpage\.com$
RewriteRule ^(.*)$ [mywebpage.com?nodomain=$1...] [R=301,L]

From what I recall, in perl regular expressions the DOT means ANY character except \n (new line). So... i've relpaced that with an \. .

You should add another rewrite cond like:

RewriteCond%{HTTP_HOST}^!www\.mywebpage\.com$ [OR]

on top on top of that so, it won't afect your www.mypage.com.

Well... this is about all... If it doesn't work (I'm reffering to the rewrite rules, try consulting the net a little, though... I think it'll work.)

Regards,