Forum Moderators: phranque
My apologies if this has already been answered, I couldn't find a related discussion.
I've successfully implemented "virtual subdomains" via:
# Turn rewriteEngine on
rewriteEngine On
#
# The rules are processed in the following order: If the rewriteRule matches
# the rewriteCond directives are evaluated in the order they appear. Since
# the rewriteRule will always match eventually even if we just request / the
# rewriteCond will be evaluated.
#
# rewrite only when Host is not empty
rewriteCond %{HTTP_HOST}!^$
# rewrite only when Host is not main domain
rewriteCond %{HTTP_HOST}!^(www\.)?example\.com$ [NC]
# Extract subdomain and first path element
rewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC]
# rewrite only when subdomain not equal to first path element
rewriteCond %2<->%3!^(.*)<->\1$ [NC]
# rewrite to /subdomain/request
rewriteRule ^(.+) /%2/$1 [L]
But ... is it possible to reference resources in other same-level or parent folders? for example:
sub.example.com -> example.com/sub/
Directory structure:
/images/
/scripts/
/sub/
In this scenario, the /sub/index.php file contains references such as "../images/image.jpg" or "../scripts/script.php" which, of course don't work; instead the browser url's look like:
"http://sub.example.com/images/image.jpg" which, on the server is rewritten to "http://example.com/sub/images/image.jpg"
Instead, I want it rewritten to "http://example.com/images/image.jpg"
One solution is to change the file-path references to a full-url, such as "http://www.example.com/images/image.jpg" or "http://www.example.com/scripts/script.php"
Other than that,
QUESTION: is it possible to change my directory structure and/or provide additional rules to allow translation of "../" into the parent folder?
"http://sub.example.com/images/image.jpg" which, on the server is rewritten to "http://example.com/sub/images/image.jpg"
Instead, I want it rewritten to "http://example.com/images/image.jpg"
In fact, you don't probably want it rewritten at all. And you can't rewrite to a 'domain' anyway -- A rewrite results in an internal filepath, not a URL, and domains and subdomains don't exist inside the server; There are only directories and files.
So, you may wish to add exclusions to your subdomain-to-subdirectory rule so that the /image and script requests are not rewritten to the subdomains' subdirectories. Something like:
RewriteCond %{REQUEST_URI} !^/images/
RewriteCond %{REQUEST_URI} !\.php$
[edited by: jdMorgan at 5:55 pm (utc) on Nov. 20, 2007]