Should this be php script or controlled with .htaccess file?

Hi,

I am directing users from here:
url : mypage.example.com

to here : example.com/mypage/

BUT if the directory “mypage” does not exist,
then I want to direct the user to

to: example.com/info.php?pg=mypage

( Obviously “mypage” has many variations )

I don’t think I can put if statements in the .htaccess file

but how would I do this in php script ?

Any suggestions on how to do this ?

Thanks.

.

You can actually do this in .htaccess! (and you should, because it’s faster than doing it in PHP)


Options +FollowSymlinks -MultiViews

RewriteCond %{HTTP_HOST} ^(.*)\\.example\\.com$ [NC]
RewriteCond %1 -d
RewriteRule .? http://example.com/%1/ [L,R=302]

RewriteCond %{HTTP_HOST} ^(.*)\\.example\\.com$ [NC]
RewriteRule .? http://example.com/info.php?pg=mypage [L,R=302]

Once you’re happy with how it works replace R=302 by R=301
Make sure you handle www. before you do any of this, otherwise you’ll be rewriting www as well (you can add `RewriteCond %1 !www as a second RewriteCond to both blocks to prevent www from being handled by these blocks).

Ah, actually, I don’t think that will work because subdomains are actually separate domains. Translated, you need to declare your subdomain names in order for the host’s DNS to send the request to your domain.

If the host has such a trick up their sleeve, then Rémon’s code should work (if the *'s are replaced with +'s, and the last line’s mypage replaced with %1 [the atom generated by the EVERYTHING atom in the attached RewriteCond statement]).

Regards,

DK

The easiest way to do this is with wildcard DNS. Most respectable DNS daemons support this.

Rémon,

Bingo! That would do it on the DNS side. With the mods to your mod_rewrite code, jekko should be in business.

Regards

DK