Re: mod_rewrite virtual hosting question...

Top Page
Attachments:
Message as email
+ (text/plain)
Delete this message
Reply to this message
Author: Kurt Granroth
Date:  
To: Main PLUG discussion list
Subject: Re: mod_rewrite virtual hosting question...
On Wednesday 24 May 2006 15:23, jon wrote:
> I'm totally confused when it comes to mod_rewrite, but here's what I'm
> trying to do -- redirect any instance of a given domain to a specific
> folder.
>
> So.... let's say I want to visit example.com/index.php, the request
> should go to /var/www/sites/example.com/index.php
>
> What I'm looking for is something flexible -- I don't want to have to
> add vhosts everytime we add a domain to the server, just a site.com
> folder in /var/www/sites


I've appended the top-level .htaccess file that I use for this purpose. I
don't remember where I got the core of this but it's custom modified to work
for my setup. Basically, it parses out each individual component of the URL
to a separate sub directory. This file is in:

/home/user/public_html/.htaccess

If a URL comes in like 'domain.com/index.php', then it looks here:

/home/user/public_html/domain.com/index.php

A URL like 'sub.domain.com/index.php' would redirect to here:

/home/user public_html/domain.com/sub/index.php

And even 'sub.domain.com/dir/tree/index.php' works:

/home/user/public_html/domain.com/sub/dir/tree/index.php

Note that some 3rd party PHP scripts will cause issues since the PHP 'SELF'
variables and similar will totally ignore your domain name and focus on the
internal directory structure. But for 99% of the cases, it works great.

.htaccess:
==========
RewriteEngine on
RewriteBase /

# Prevent problems caused by missing trailing slash
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^/*([^/\.]+\.[^/]+/)?(.*[^/])$ http://%{HTTP_HOST}/$2/ [R=301,L]

# These lines correct some of badly formed requests.
RewriteCond %{THE_REQUEST} ^[^\ ]+\ /([^/\.\ ]+\.[^/\ ]+)/
RewriteRule ^/*([^/\.]+\.[^/]+)/(.*)$ http://$1/$2 [R=301,L]

# Generic sub / pointed domain code (core part)
RewriteRule ^/*[^/\.]+\.[^/]+/ - [L]

# Match my.domain.org -> DOCUMENT_ROOT/my.domain.org
RewriteCond %{DOCUMENT_ROOT}/%{HTTP_HOST} -d [OR]
RewriteCond %{DOCUMENT_ROOT}/%{HTTP_HOST} -l
RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [L]

# Match my.domain.org -> DOCUMENT_ROOT/domain.org/my
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+\.[^\.]+.*)$
RewriteCond %{DOCUMENT_ROOT}/%2/%1 -d
RewriteRule ^(.*)$ %2/%1/$1 [L]

# Match no.domain.org -> DOCUMENT_ROOT/domain.org
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+\.[^\.]+.*)$
RewriteCond %{DOCUMENT_ROOT}/%2 -d
RewriteRule ^(.*)$ %2/$1
---------------------------------------------------
PLUG-discuss mailing list -
To subscribe, unsubscribe, or to change you mail settings:
http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss