Forum Moderators: phranque

Message Too Old, No Replies

Rewriting hostname into a path component

compatible with Drupal's Clean URLs...

         

Transhumanist

5:21 pm on Oct 27, 2011 (gmt 0)

10+ Year Member


Hi all,

I'm trying to set up a redirection such that a request for

xyz.example.com/some/path

is transparently (i.e. the user still sees the original URL) routed to

www.example.com/xyz/some/path

The complication being that this is a Drupal site with Clean URLs, so there are already some rewrite rules in place:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

(The purpose of these, for those not familiar with Drupal, being to convert www.example.com/nice/url internally to www.example.com/index.php?q=nice/url)

I tried adding the following in between the first and second lines above, but it appears to do nothing at all - xyz.example.com/some/path delivers exactly the same page as www.example.com/some/path:

RewriteCond %{HTTP_HOST} ^xyz\.example\.com
RewriteRule ^(.*)$ xyz/$1 [QSA]

Can anyone explain why this doesn't work, and perhaps point me towards a better solution?

Thanks in advance!

-Ben

lucy24

7:17 pm on Oct 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Where in your Rule is xyz.example.com getting changed to www.example.com? I only see the /xyz being inserted.

In general, changes in (sub)domain involve evil business with Proxies if you want to keep it as a Rewrite rather than a Redirect.

g1smd

7:23 pm on Oct 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



When you change a reference internally, that reference is just folders, files and paramters.

Domain names are used in URLs out there on the web.

There is no concept of a domain name inside the server.

Transhumanist

7:35 pm on Oct 27, 2011 (gmt 0)

10+ Year Member



Lucy - it isn't, but unless I'm missing something that shouldn't actually matter, since they both point to the same IP. I'd be happy if it would just insert a leading /xyz (so the effective URL ends up being xyz.example.com/xyz/some/path).

I think it needs to be a rewrite, unless there's some way to redirect without the user's URL changing?

g1 - I'm not sure what you're saying - surely the HTTP_HOST variable gives the server access to the domain name that was requested...?

g1smd

7:38 pm on Oct 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



convert www.example.com/nice/url internally to www.example.com/index.php?q=nice/url


This is what I am saying...

convert www.example.com/nice/url internally to /index.php?q=nice/url


Domain names are not used inside the server, only paths and files.

Additionally, slashes are NOT a valid character within parameters.

Transhumanist

8:03 pm on Oct 27, 2011 (gmt 0)

10+ Year Member



Ah, I see - thanks for the clarification, but that's not the problem; those lines are supplied "as is" as part of Drupal's setup, and have always worked just fine (I assume the disallowed slashes get escaped somewhere along the way).

I'm only including them here because whatever is added to accomplish the subdomain -> path conversion needs not to break them :-)

lucy24

1:37 am on Oct 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The original post said this:

xyz.example.com/some/path

is transparently (i.e. the user still sees the original URL) routed to

www.example.com/xyz/some/path


so pretty clearly a Rewrite was intended, but where does the xyz change into www (or vice versa)?

Transhumanist

10:14 am on Oct 28, 2011 (gmt 0)

10+ Year Member



Hi Lucy,

You were quite correct to point out that the lines I inserted don't change the subdomain from xyz to www; as I said, though, since they point to the same IP that shouldn't actually matter so long as the leading /xyz is inserted into the path if and only if the subdomain originally requested was xyz. Or is there a reason it would still matter, that I'm not aware of?

I'd really appreciate if someone could explain how to accomplish this...!

lucy24

8:26 pm on Oct 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If abc.example.com and www.example.com serve the same content you may have a Duplicate Content issue. But that's outside my-- haha-- domain.