Closed Thread Icon

Topic awaiting preservation: A common yet confusing .htaccess url redirection question... (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=22340" title="Pages that link to Topic awaiting preservation: A common yet confusing .htaccess url redirection question... (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: A common yet confusing .htaccess url redirection question... <span class="small">(Page 1 of 1)</span>\

 
smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 06-27-2004 13:11

Ok,

I have a host that allows unlimited subdomains, and also allows multiple domains to be assigned to the one root directory:

i.e.


www.bob.com
www.harry.com
www.freddy.com


can all point to my root directory - not forwarding, but properly assigned.

However, this means that www.bob.com/dir1/index.htm is exactly the same as www.harry.com/dir1/index.htm

What I would like to do is esentially set the base directory for each domain to a different directory, my structure looks like this:

root
|
|______bobDir
|
|______harryDir
|
|______freddyDir

and then if someone went to www.bob.com they would seamlessly and invisbly be sent to bobDir etc. But rather than it looking like www.bob.com/bobdir I would rather it was just www.bob.com - essentially just specifying a different root directory for each domain.

I have looked around and found this:

code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^cindy1\.localdomain\.com$
RewriteRule ^/(.*) http://cindy2.localdomain.com/$1 [R,L]

Would this work and is this the best way to do something like this?

Thanks guys,

Jon

SCRWD//MULTIMEDIA PORTAL

(Edited by smonkey on 06-27-2004 13:13)

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 06-27-2004 13:16

aehm... the best way would be for your domains to just point to the different directories,
but yes it could be done with RewriteRules - though you'd leave out the R (redirect) and wouldn't direct to a different server.
You'd just do
RewriteRule ^/(.*) /bobDir/$1 [L]
instead.

smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 06-27-2004 13:25

Would that be invisble or would the bobDir actually appear in the address bar (the idea is to avoid that all together)

I found this on http://httpd.apache.org/docs/mod/core.html#documentroot :

quote:
DocumentRoot directive
Syntax: DocumentRoot directory-path
Default: DocumentRoot /usr/local/apache/htdocs
Context: server config, virtual host
Status: core

This directive sets the directory from which httpd will serve files. Unless matched by a directive like Alias, the server appends the path from the requested URL to the document root to make the path to the document. Example:

DocumentRoot /usr/web

then an access to http://www.my.host.com/index.html refers to /usr/web/index.html.

There appears to be a bug in mod_dir which causes problems when the DocumentRoot has a trailing slash (i.e., "DocumentRoot /usr/web/") so please avoid that.



Is that any good for anything?

SCRWD//MULTIMEDIA PORTAL

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 06-27-2004 13:56

yeah, but you shouldn't be able to set documentRoot within .htaccess - only within the real server configuration.


If you don't add [R] (which means redirect) to it, the page will appear to come from the location the user typed in.
(This is how it's done here).

smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 06-27-2004 14:26

Thanks buddy - one day I think I will buy a book on apache and htaccess since it is so useful yet I never get round to learning it properly. Online resources are good, but either not concise enough, or the ones that are concise are also too 'hi-brow' for a lowly designer/developer like myself.

Thanks again - the asylum rules, always here when I need help.

Jon

SCRWD//MULTIMEDIA PORTAL

smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 06-28-2004 22:11

***Edit***

Ok I know have this which sorta work but doesn't (seems to cause some werid kind of infinite loop lol)

code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.scrwd.com$
RewriteRule ^(.*)$ /comDir/$1

RewriteCond %{HTTP_HOST} ^www.scrwd.net$
RewriteRule ^(.*)$ /netDir/$1

RewriteCond %{HTTP_HOST} ^www.jomason.com$
RewriteRule ^(.*)$ /joDir/$1



What is wrong there?


SCRWD//MULTIMEDIA PORTAL

(Edited by smonkey on 06-29-2004 00:39)

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 06-29-2004 16:13

Hm... try adding a [L] - signifing -last rule to apply to your rewriteRules and see if it still runs into the loop.

smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 06-29-2004 18:33

I have tried that, I've also tried escaping the . too

hmmm, it's puzzling - also I could only get it working for the first rewrite - the second two seemed to do nothing (although upon refresh, every other time it would throw up an error page saying that I couldn't access www.scrwd.com/comDir/comDir/comDir/comDir/comDir/comDir/... etc and hence I figured some kinda bad looping was going on.

Ok so back to basics (and no more example urls):

I own:

www.scrwd.com
www.scrwd.net
www.jomason.com

they all have the nameservers assigned the same and point to the same root directory on that server.

inside that root directory I want to put a directory of each of the sites above and essentially make these directories the new 'roots' for my domains.

I have only the mythical power of .htaccess support to help me do this - so what from scratch needs to be done...

any ideas?

I'm stuck

Thanks guys,

Jon

SCRWD//MULTIMEDIA PORTAL

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 06-29-2004 19:03

hm... give this a try

code:
RewriteEngine On

RewriteCond %{HTTP_HOST} ^www.scrwd.com$
RewriteRule ^comDir/(.*)$ /comDir/$1 [L]
RewriteRule ^(.*)$ /comDir/$1 [L]

RewriteCond %{HTTP_HOST} ^www.scrwd.net$
RewriteRule ^netDir(.*)$ /netDir/$1 [L]
RewriteRule ^(.*)$ /netDir/$1 [L]

RewriteCond %{HTTP_HOST} ^www.jomason.com$
RewriteRule ^joDir(.*)$ /joDir/$1 [L]
RewriteRule ^(.*)$ /joDir/$1

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana, USA
Insane since: Aug 2000

posted posted 06-29-2004 21:12

Try the following:

code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(ww+\.)?scrwd\.com
RewriteCond %{REQUEST_URI} !/comDir/
RewriteRule ^(.*)$ /comDir/$1 [L]
RewriteCond %{HTTP_HOST} ^(ww+\.)?scrwd\.net
RewriteCond %{REQUEST_URI} !/netDir/
RewriteRule ^(.*)$$ /netDir/$1 [L]
RewriteCond %{HTTP_HOST} ^(ww+\.)?jomason\.com
RewriteCond %{REQUEST_URI} !/joDir/
RewriteRule ^(.*)$$ /joDir/$1 [L]



-- not necessarily stoned... just beautiful.

smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 06-29-2004 21:52

Hyperbole that seems to have worked great,

do you know what is happening there?

how it works?

what is the {REQUEST URI} bit for?

why do some of the RewriteRule's have double $$ and the top one only has 1?

Thanks buddy,

Ty I haven't tried yours, maybe it works, but I immediately went for the last post which was Hyperbole's assuming that usually by the end of a thread the answer gets 'more right'. Thanks for all your help tho, it is much appreciated.

Jon

SCRWD//MULTIMEDIA PORTAL

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana, USA
Insane since: Aug 2000

posted posted 06-30-2004 23:46

smonkey: The double dollar signs are misprints. Change them to read ^(.*)$. If what I gave you worked then Apache must be smart enough to realize you can only get to the end of a line once.

The line

code:
RewriteCond %{HTTP_HOST}    ^(ww+\.)?scrwd\.com

tells Apache to look at the HTTP_HOST variable starting at the beginning look to see if it contains ww, www, wwww, etc followed by a dot. This is optional (the ? following the parentheses makes it optional. This means this line will accept scrwd.com, ww.scrwd.com, www.scrwd.com, wwww.scrwd.com, etc.

If the first condition passes Apache looks at the REQUEST_URI which will contain the entire path to the page requested and check to see if it contains the string '/comDir/'. If the REQUEST_URI contains '/comDir/' the URI has already been through this loop once and has had the appropriate directory appended so don't do it again (This is the place you were getting the infinite loop).

If the REQUEST_URI does not contain '/comDir/', the Rewrite Conditions both succeed so use the entire REQUEST_URI to create a field (that's what ^(.*)$ does). Apache prepends /comDir/ to the REQUEST_URI and repeats the entire loop.

For example:
Apache receives a request for scrwd.com/some-dir/index.html
HTTP_HOST contains scrwd.com
REQUEST_URI contains /some-dir/index.html
The first RewriteCond matches scrwd.com in HTTP_HOST
the second RewriteCond succeeds because REQUEST_URI doesn't contain /comDir/
The RewriteRule matches /some-dir/index.html and marks the entire URI as field $1 and changes the REQUEST_URI to /comDir/some-dir/index.html. The [L] tells Apache it doesn't need to check any more rules so the entire request is rechecked starting at the top.

This time
HTTP_HOST is scrwd.com
REQUEST_URI is /comDir/some-dir/index.html
The first RewriteCond succeeds
The second RewriteCond fails because the REQUEST_URI contains /comDir/
Apache skips the RewriteRule and checks the next RewriteCond which fails etc.

Finally Apache looks for the file /comDir/some-dir/index.html and if it exists hands it back to the client that requested the file in the first place.

-- not necessarily stoned... just beautiful.

smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 07-01-2004 11:55

Cool,

Thanks buddy.



SCRWD//MULTIMEDIA PORTAL

« BackwardsOnwards »

Show Forum Drop Down Menu