Forum Moderators: phranque

Message Too Old, No Replies

subdirs in rewritten subdomains

.htaccess used to rewrite subdomain, doesn't rewrite subdirs

         

jcall

8:48 am on Jan 21, 2005 (gmt 0)

10+ Year Member



I use the following chunk in .htaccess to provide access to my /testing directory as testing.mydomain.com.

RewriteCond %{HTTP_HOST} testing.mydomain.com
RewriteCond %{REQUEST_URI}!testing/
RewriteRule ^(.*)$ testing/$1 [L]

That works beautifully. I'm using the /testing directory, and the testing.mydomain.com subdomain as a testing area where I can build/test on my website without danger of messing up the "live" website.

Problem is, though, that when I browse to testing.mydomain.com/subdir1, I really get the contents of www.mydomain.com/subdir1.

As an example, I need to be able to access the text file /testing/subdir1/textfile.txt as testing.mydomain.com/subdir1/textfile.txt. Presently, testing.mydomain.com/subdir1/textfile.txt will return a 404 since the file does not exist in /subdir1 , only in /testing/subdir1/ .

I've looked at Apache's docs on mod_rewrite and .htaccess, but am not sure how to proceed.

Any suggestions?

johnt

4:24 pm on Jan 21, 2005 (gmt 0)

10+ Year Member



Try looking into RewriteBase in the Apache docs. If memory serves, I think that this will help you do want you want to.

Cheers

John

jdMorgan

5:10 pm on Jan 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A couple of ideas:

First, make sure you disable MultiViews unless you are using them.
Second, try enabling RewriteOptions Inherit in /testing/subdir -- Inherit may not be set in your default server config.

I'd also suggest some cleanups/speedups on your code:


RewriteCond %{HTTP_HOST} ^testing\.mydomain\.com
RewriteCond %{REQUEST_URI} !^/testing/
RewriteRule (.*) /testing/$1 [L]

Jim

jcall

1:16 am on Jan 22, 2005 (gmt 0)

10+ Year Member



I do also have the following:

Options All -Indexes +FollowSymlinks

RewriteEngine On
RewriteBase /

Thanks for the tips - - I'll look into RewriteOptions Inherit momentarily.

-jared

jcall

1:23 am on Jan 22, 2005 (gmt 0)

10+ Year Member



no joy with RewriteOptions Inherit. I put it in /testing/subdir1, but when I browse to testing.mydomain.com/subdir1, I still get the contents of /subdir1 (which is also displayable as www.mydomain.com/subdir1).

Back to the drawing board . . .

-jared

jdMorgan

5:15 pm on Jan 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jared,

Maybe I'm not clear on what you expected or want to happen.

After resolving to your IP address and being processed through http.conf, the "subdomain name" pretty much loses its meaning. Inside the server, there are no domains, subdomains or URLs -- there are only directories, subdirectories, and files. In either httpd.conf or in .htaccess, the entire URL is translated into a file system path. So, your "domain name" is discarded having reached the server, the subdomain becomes a subdirectory (in typical configurations), and is therfore accessible via any URL that will navigate into your server's Web-acessible filesystem.

If you want to prevent "direct access" via www.mydomain.com/subdir1, then use RewriteCond %{THE_REQUEST} to test that the browser-requested URL is *not* www.mydomain.com/subdir1. Other code, depending on %{REQUEST_URI} and the URI seen by RewriteRule, will not examine the requested URI, but rather the rewritten URI (if any rewriting has already occurred). However, %{THE_REQUEST} is not affected by rewriting -- it always contains the request sent by the browser.

See this related recent thread [webmasterworld.com].

Jim

jcall

7:37 pm on Jan 22, 2005 (gmt 0)

10+ Year Member



Let me state this a different way. www.mydomain.com is docrooted at /homedir/user/www. I would like testing.mydomain.com to be docrooted at /homedir/user/www/testing.

www.mydomain.com/abc/file.txt should retrieve /homedir/user/www/abc/file.txt.

testing.mydomain.com/abc/file.txt should retrieve /homedir/user/www/testing/abc/file.txt.

I know that this is possible - - this is what setting up a "subdomain" in cPanel does. cPanel does not use .htaccess to accomplish this, as I never saw any changes to my .htaccess files when I was hosting with a cPanel host.

My current host does not use cPanel, and I'm trying to figure out if this is possible to accomplish with .htaccess. Yes, I can pay them to do it for me, but I'd *really* like to know how to do this myself.

Perhaps this is not possible with .htaccess.

Thank you for your assistance.

-jared

jdMorgan

7:59 pm on Jan 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, I don't know what the problem is. I use pretty much the same code as shown in msg#6 to implement my testing subdirectories, which behave as you describe. I have both a "dev" and "test" subdirectory, though, and I use a back-reference to prevent a loop instead of using %{REQUEST_URI}, but the method is identical. Here's the code from a live server:

RewriteCond $1 !^(devŠtest)/
RewriteCond %{HTTP_HOST} ^(devŠtest)\.example\.org
RewriteRule ^(.*) /%1/$1 [L]

The most frequent problem I've ssen is on sites which *do* have a cPanel interface, and the problem is that the cPanel-produced code (in httpd.conf) is set up such that the .htaccess file where the above code has been added is not in the directory-path to the subdomain-subdirectory, and so is not executed.

Replace the broken pipe "Š" characters above with solid pipes before attemptiing to use this code.

Jim

[edited by: jdMorgan at 8:16 pm (utc) on Feb. 23, 2005]

jcall

6:52 am on Jan 23, 2005 (gmt 0)

10+ Year Member



Thanks for the work and advice, but I think I'm stuck. It must be something that the host has done with httpd.conf. I'll just have to get them to set it up for me.

Wish I could have figured it out, but at this point I can't wait any longer. :(

Thanks again.

-jared