Forum Moderators: phranque

Message Too Old, No Replies

Subdomain mod_rewrite root directory problem

How to only allow subdomain access to new root folder?

         

jexx

7:26 pm on Jun 2, 2004 (gmt 0)

10+ Year Member



I have a properly functioning "testing" subdomain located in a 'test' directory and it works fine.

.htaccess entry as follows:

#Redirect to testing subdomain
RewriteCond %{HTTP_HOST} ^test\.domain\.com
RewriteCond %{REQUEST_URI}!^/test/
RewriteRule ^(.*) /test/$1 [L]

I can access the testing site at test.domain.com without problems, however, test.domain.com/test/ will ALSO point to the same location!
It seems like its using the original document root if the requested URL is not found relative to the subdomain root located at /test/

If I, on the other hand, create a test tmp subdirectory with DIFFERENT content under /test/, test.domain.com/tmp/ correctly displays the content. However, so does test.domain.com/test/tmp/

What I want to do is restrict the subdomain to /test/ as the root folder. It seems like right now it is ambiguous.

Any help would be appreciated..

jdMorgan

8:53 pm on Jun 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jexx,

You could do it "cleanly" using DocumentRoot in http.conf. However, using mod_rewrite in a per-directory (.htaccess) context, what you're seeing with the two URLs reaching the same context is "just the way it is."

Note that as long as you always refer to the subdomain without the subdirectory in your links, no-one will know that the subdirectory exists, so this may not be a big deal. If it is, then define the subdomain as a separate virtual server in httpd.conf and this problem will go away.

Jim

jexx

9:24 pm on Jun 2, 2004 (gmt 0)

10+ Year Member



Thanks Jim. I know that setting doc root for a virtual host would solve this problem.
Just wanted to see if there was a 'htaccess-based' way to do this.

Also, I omitted a line in my original post not to confuse the issue. This is what I have:

#Redirect to testing subdomain
RewriteCond %{HTTP_HOST} ^test\.domain\.com
RewriteCond %{REQUEST_URI}!^/test/
RewriteCond %{REQUEST_URI}!^/images/
RewriteRule ^(.*) /test/$1 [L]

I want all image links to be retrieved from the SAME images directory as for the 'production' system.
Simply excluding the images directory is *not* doing the trick completely.

All images use paths relative to the document root, e.g. <img alt='imagename' src='images/imagename.jpg'>.

What is the best way to do this so that all images are retrieved from the same images directory?

jexx

9:26 pm on Jun 2, 2004 (gmt 0)

10+ Year Member



should be <img alt='imagename' src='/images/imagename.jpg'>

that is what i have right now.

jdMorgan

10:21 pm on Jun 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jexx,

It's like you've posted code off one of my sites...

I use the following code for exactly the same purposes you state - support test directories, but share images between test and production:


# Redirect <subdomain>.example.org/<path> to www.example.org/<subdomain>/<path>
# Skip rewrite if no hostname, if the requested subdomain is www, or if image request
RewriteCond %{HTTP_HOST} !(^$¦^www\.) [NC]
RewriteCond %{REQUEST_URI} !^/images/
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.example\.org(:80)?<>/([^/]*) [NC]
RewriteCond %1<>%3 !^(.*)<>\1$ [NC]
RewriteRule ^(.*) /%1/$1 [L]

The only difference here is that my ruleset supports multiple subdomains (test1, test2, etc.).

The code above contains a 'trick' to allow an exclusion check for *any* subdomain matching its related subdirectoy, which is normally not possible. The trick is that "\1$" back-references the immediately-preceding "(.*)" in the 4th RewriteCond. So if HTTP_HOST + requested_subdirectory = HTTP_HOST + HTTP_HOST, then the subdirectory name equals the HTTP_HOST name, and we don't need to redirect. This is just another way to do the
RewriteCond %{REQUEST_URI} !^/test/
function of your code, but for any subdomain.

Other than that trick, our code is essentially identical. And unfortunately, I can't see why yours won't work. :(

Jim