.htaccess change subdomain and subfolder to parameter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nakeddeveloper
Forum Newbie
Posts: 4
Joined: Wed Feb 25, 2009 10:58 am

.htaccess change subdomain and subfolder to parameter

Post by nakeddeveloper »

I would like to have a .htaccess file that will passively redirect
subdomain.domain.com/folder (with or without a / at the end)
to
"http://www.domain.com/index.php?subdoma ... der=folder"

I would also like it leave it as it is when a file is requested.

Please help, I've seen similar stuff about but I think I just need it all spelling out for me... something in my head is about to pop...
nakeddeveloper
Forum Newbie
Posts: 4
Joined: Wed Feb 25, 2009 10:58 am

Re: .htaccess change subdomain and subfolder to parameter

Post by nakeddeveloper »

For anyone who is interested, I have found what I believe is a more elegant solution by moving the functionality to php...
The .htaccess file just sends everything to index.php now...

Code: Select all

RewriteEngine On 
RewriteRule ^(.*)$ index.php
And this is my php in the index.php file

Code: Select all

<?php
$uri = $_SERVER['REQUEST_URI'];
$domain_parts = explode('.',$_SERVER['HTTP_HOST']); 
if (count($domain_parts) == 3 && $domain_parts[0]!= "www")
  {                         // make sure a subdomain is called 
    $subdomain = $domain_parts[0];
  }
?>
I may still need some help as there are likely going to be issues when refering to images or ajax scripts, watch this space...
Last edited by Benjamin on Tue May 26, 2009 10:37 am, edited 1 time in total.
Reason: Changed code type from text to php.
Post Reply