Wildcard subdomain redirect?

Using a single web hosting account to host multiple sites

Wildcard subdomain redirect?

Postby JohnnyK » Fri Apr 27, 2007 4:20 am

Hi guys,

I am new here and new to apache. I have been searching all over the web and I thought you guys could help me out.

I have several sub domains where it may be easy to mistype. I therefore setup a wildcard DNS so my site catches everything with my domain. The problem is I want to alert people they made a mistake. Currently the wildcard sends you to my homepage but I would like to send wrong url's to an “error” page letting people know they should check their typing.

I have a wildcard DNS and I would like mistyped or wrong urls directed to the error.php page rather than to the home page. All urls are currently re written without the www so I am using the following code to accomplish this.

RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

For example: demo is an active subdomain so if demo.domain.com or www.demo.domain.com is typed you will go to the proper page (being demo.domain.com since I am eliminating all www) but if demmmmo.domain.com (or www version) is typed you will go to the error.php page.


I would appreciate any help as I have been going crazy tring to figure this out.

Thanks,
John
JohnnyK
 
Posts: 5
Joined: Fri Apr 27, 2007 4:03 am

Postby richardk » Fri Apr 27, 2007 10:11 am

Where do sub domains exist go? To a sub directory? Is the sub directory named the same as the sub domain name.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby JohnnyK » Mon Apr 30, 2007 10:12 am

The path to the subdomains is to a directory on my server.

For example: demo.domain.com can also be accessed through domain.com/subdomain/demo
JohnnyK
 
Posts: 5
Joined: Fri Apr 27, 2007 4:03 am

Postby richardk » Mon Apr 30, 2007 11:07 am

Try this
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} ^(.+)\.([^\.]+\.[^\.]+)$
RewriteCond %{DOCUMENT_ROOT}/subdomain/%1/ !-d
RewriteRule .* /error.php [L]


However, you probably can't rewrite to error.php (because it's above /subdomain/<sub>/), so you may have to replace the last rule with a redirect
Code: Select all
RewriteRule .* http://%2/error.php [R,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby JohnnyK » Wed May 02, 2007 3:38 am

I wanted to post back so that anyone else that stumbles on to this would know what worked. I used the code as written except as richardk suggested I substituted the last line with


RewriteRule .* http://%2/error.php [R,L]

The code did exactly what I needed. If you type in a subdomain that does not exist I can still capture it but direct it to a page where I can inform people that they arrived here due to a mistype or improper subdomain.

Thanks allot richardk I very much appreciate the help.

Thanks again,
Johnny K
JohnnyK
 
Posts: 5
Joined: Fri Apr 27, 2007 4:03 am


Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 116 guests

cron