Forum Moderators: phranque

Message Too Old, No Replies

Problem in mod rewrite for subdomain

we have problem when we use virtual subdomain using mod rewrite

         

himanshu

12:16 pm on Jun 22, 2007 (gmt 0)



Hello,

I am totally new for LAMP platform. Our company has a website where we use subdomains through mod rewrite. We have a folder "subd" in main root directory and with in this folder we have another folders like forum, blog etc which we use for subdomain.

Our problem is when we type in browser like [forum.example.com...] then its work but when we type [forum.example.com...] then it not work and show the main page of website www.example.com.

I want that in both cases(with or without www)we go forum section.

Here my code for rewrite

RewriteEngine On

rewriteCond %{REQUEST_URI}!^/subd
rewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com
rewriteCond /home/example/public_html/sudb/%1 -d
rewriteRule ^(.*)$ /sudb/%1/$1 [l]

The code of home page of the website

<?php
$domain = "example.com";

$sub = $_SERVER['HTTP_HOST'];

$sub = eregi_replace("\.".$domain, "", $sub);
$sub = eregi_replace("www\.", "", $sub);
$sub = strtolower($sub);
$docroot = $_SERVER['DOCUMENT_ROOT'];
if (is_dir("$docroot/$sub"))
{
header("Location: [$domain...]
exit;
}
else
{
include "index-main.php";
}
?>

I don't know the connection between both two codes.

I appriciate any help in this regard.

Thank you to all in advance.

Himanshu

jdMorgan

1:51 pm on Jun 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The fix is to allow the "subdomain-name-extraction" RewriteCond to recognize --and to discard-- a www prefix if it is present. This requires a minor change in the following two lines, because the subdomain is now stored in variable %2 instead of %1.

Another problem with the original code is that it will map "www.example.com" to the subdirectory www if it exists. If it that subdirectory does not exist and will never exist, then a lot of time is wasted checking the filesystem to see if it exists -- even though the end result is that it is always mapped to the main domain space in the end. A new RewriteCond addresses this problem.


RewriteCond %{REQUEST_URI} !^/subd
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteCond /home/example/public_html/sudb/%2 -d
RewriteRule (.*) /sudb/%2/$1 [L]

Note that for portability between servers, you may be able to substitute the server variable %{Document_Root} for all or part of the path "/home/example/public_html".

Jim

himanshu

8:25 am on Jun 23, 2007 (gmt 0)



Thank you Jim for your time and reply. I tried with your code but now in all cases I am going to www.example.com.

I appriciate any further help in this regard.

Himanshu

himanshu

9:37 am on Jun 23, 2007 (gmt 0)



Sorry Jim, Your code is working as per my requirments. I had a typo mistake previously.

Many thanks to you. You rock man.

Himanshu