Forum Moderators: phranque

Message Too Old, No Replies

using mod_rewrite to route multiple domains on same server

can I remove the /subdir/ and still have it work?

         

pbarney

7:48 pm on Oct 6, 2004 (gmt 0)

10+ Year Member



Hello everyone! This is my first post to webmasterworld.com.

I've been looking high and low for an answer to this question, and although I've found some great information, I've yet to find anything that addresses my specific issue.

1. I have multiple domains that point to the same server.
2. I do not have access to httpd.conf, so they aren't virtual hosts, they're just add-on domains.
3. I want to do this:

http://www.foo.com/anydir/anyfile.html

to pull this file:

http://www.foo.com/[b]foo/[/b]anydir/anyfile.html

BUT, here's the catch:

I still want the user's broswer location bar to display

http://www.foo.com/anydir/anyfile.html
, without the foo/ directory displaying in the url.

Currently, I'm just using a php script as my index file to check the which http_host was called and change the location to point to the right directory, to wit:

$host = getenv(HTTP_HOST);
if ($host == "www.foo.com") { header("Location:
http://www.foo.com/foo/index.html"); }
if ($host == "www.bar.com") { header("Location:
http://www.bar.com/bar/index.html"); }

But this doesn't solve my desire to want to eliminate that /foo/ directory from showing up in the URL.

Is this even possible with mod_rewrite? If so, I'd really appreciate if anyone can help me solve this.

Peter B.
Columbus, Ohio USA

jdMorgan

10:37 pm on Oct 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Peter,

Welcome to WebmasterWorld!

This thread demonstrates a method to rewrite any arbitrary subdomain request to a like-named subdirectory [webmasterworld.com] using mod_rewrite.

Jim

pbarney

2:01 pm on Oct 7, 2004 (gmt 0)

10+ Year Member



Hi JIm! Thank you for the quick response.

I looked at the thread you referenced, but it doesn't seem to apply to me for this reason:

I'm not trying to redirect subdomains, I'm trying to redirect domains, preferably without looking like any redirection is being done. Is it possibly for appached to just "assume" that anything requested from foo.com should be served from a specific directory, without indicating that directory in the url? (just as requests to bar.com should come from it's own directory?)

Thanks again!

jdMorgan

7:14 pm on Oct 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you read and understand that code, you'll be able to modify it to suit your needs. The code does not do external redirects, it simply rewrites the URL so that content is provided from a subdirectory corresponding to the requested subdomain. As such, no handshaking with the client is involved, so the URL in the client's address bar does not change.

In your case, you'll need to modify it so that content is provided based on the requested domain, rather than subdomain. This assumes that all of your domains are pointed to the same Document_Root in httpd.conf, or that you place the code itself in httpd.conf, with appropriate changes.

The important concept to use from the code I cited is that it must check to be sure that the request is not already being made to the subdirectory. If you do not include that function, then you introduce the possibility of an "infinite" rewrite loop.

Jim

wudan

1:40 am on Oct 12, 2004 (gmt 0)



I am slightly confused as to how the subdomain problem solution can be used for pbarneys problem (which incidentally is the same as mine). Ive been searching all over the web for a solution and this is the first place I got close but not quite close enough (at least for my level of understanding...)

In addition my host gave me some code to use for this problem but I dont seem to manage to get it to work either:

RewriteEngine On
Options +FollowSymlinks
RewriteBase /
# Rewrite Rule for example.com
RewriteCond %{HTTP_HOST} www.example.com$ [NC]
RewriteCond %{REQUEST_URI} !example
RewriteRule ^(.*)$ example/$1 [L]

What I would want to do but cant manage is get domain2 to start from folder2 of domain1, but nothing I have tried seems to do the job. All I ever manage is either an error500 or an error404 or it just stays with top data from domain1... :-(

Well, now I actually got it to work, but only when I use the same filenames in folder2 as they are used for domain1, ie. the frameset-files of index.htm have to be a.htm, b.htm and c.htm and can't be x.htm, y.htm and z.htm as folder2/index.htm would actually want them to be... very strange. But when I actually look at the page source for domain2/index.htm, I still see the source for that file, eventhough the frames within actually have the names of domain1/index.htm...? Confused... All the other subsequent filenames work how I would expect them to... All this happens only when I simply go to domain2/, if I go to domain2/index.htm everything works normally again.

Should anyone know what the reason for this might be, I would be grateful for hints so that I can apply the code correctly... Thanks...

Dan

[edited by: jdMorgan at 2:43 pm (utc) on Oct. 12, 2004]
[edit reason] Removed specifics per TOS [/edit]

jdMorgan

3:00 pm on Oct 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



wudan,

Welcome to WebmasterWorld!

If you want to make this work, you will need to understand the code and what it is doing. Reference links to get you started are available in our charter [webmasterworld.com]. This is a somewhat-advanced subject, and it's difficult to make it work by simply cutting and pasting code.

Understand that the distinction between your "domains" ceases to exist after the requested URL is "translated" into a filepath within the filesystem of the server. On the Web, we have domains, URLs, and query strings. Inside the server we have directories, files, and script parameters.

mod_rewrite "lives" at the boundary between URL-space and file-space, and that's where a lot of confusion arises.

The purpose of the code you posted is to "translate" the hostname requested by the browser into a subdirectory from which files should be retrieved.

There are several problems with the code you were given, and I'd suggest the following changes:


RewriteEngine On
Options +FollowSymlinks
RewriteBase /
# Rewrite Rule for example.com
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteCond %{REQUEST_URI} !^/example/
RewriteRule ^(.*)$ /example/$1 [L]

All files needed by pages to be served in the "example" domain will have to be placed into the "example" subdomain, and the links on those pages should either be relative links canonical links pointing to the correct subdomain.

Jim