Login to download the latest version of Mint and your favorite Pepper, purchase additional licenses, or post in the Forum. Don't have an account? Create one!

In Partnership with Media Temple

Mint Forum

.htaccess or PHP trickery with subdomains

So my host, Media Temple, allows for very easy subdomains by creating a folder of subdomainA.domain.com within the “domains” folder. (This is on the Grid Server package.) Whenever you go to subdomainA.domain.com, it checks for that folder, and displays that content; if no folder exists, it displays the content for your primary pages, domain.com.

Now, if I previously used a subdomain and delete it, people are now surfing my page at subdomainA.domain.com, but seeing all the content from my primary domain. In other words, you can go to any site from my host, type in jibberish like asdf.domain.com, and be served the main pages.

My question: Any way to use .htaccess to redirect anyone from asdf.domain.com to domain.com? Remember, the .htaccess file itself is being pulled from domain.com, since the random domain does not exist (or no longer exists, in my case). Or how would this be scripted in PHP?

Sorry for the really minor detail, I’m just curious about .htaccess since I already direct www.domain.com to just domain.com; is there a catch-all?

Niclas
Minted
Posted on Jul 20, '07 at 08:50 pm

create a index.php file in your subdomain directory and add this (you don’t need no html tags) to it:

<?php header("Location: http://yourdomain.com");?>

SDJL
Minted
Posted on Jul 21, '07 at 06:50 pm

Here’s what I do. There is a slight catch with this method, in that you need to list the sub domains you do have and that you want to have work.

# Custom mod_rewrite
<IfModule mod_rewrite.c>
RewriteEngine On

# Sub Domain redirection
RewriteCond %{HTTP_HOST}            ^([^.]+).domain.com$ [NC]
RewriteCond %1 !^www$ [NC]
RewriteCond %1 !^other$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [QSA,L,R=301]

RewriteEngine Off
</IfModule>

I could be wrong, and it may work without specifying the subdomains you already have. Worth a spin anyway.

David

You must be logged in to reply. Login above or create an account