Welcome to WebmasterWorld Guest from 54.156.58.187

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Mod Rewrite subdomain to subdirectory, then access parent folder

After rewrite to sub folder, can I access other same/higher level folders?

     
4:35 pm on Nov 20, 2007 (gmt 0)

New User

5+ Year Member

joined:Nov 20, 2007
posts: 2
votes: 0


Hello!

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?

5:54 pm on Nov 20, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


It is the client (e.g. browser or robot) that resolves relative links, and that is why the "../images" link is behaving as it does. The browser resolves relative links according to the 'directory' it 'sees' in its address bar.

"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$

Jim

[edited by: jdMorgan at 5:55 pm (utc) on Nov. 20, 2007]

7:41 pm on Nov 20, 2007 (gmt 0)

New User

5+ Year Member

joined:Nov 20, 2007
posts: 2
votes: 0


Brilliant; many thanks, Jim!