htaccess rewriting wildcard subdomains to subfolders but php not working

Hi. I have a server that is using apache and php. I have wildcard subdomains working, and I am trying for this effect:
www.domain.org   goes to the normal root of the site
foo.domain.org    goes to www.domain.org/folders/foo
bar.domain.org    goes to www.domain.org/folders/bar

My folder structure is like this:
/
/css
/js
/images
/folders
/folders/foo/
/folders/foo/css
/folders/foo/js
/folders/bar/
/folders/bar/css
/folders/bar/js

There are a mixture of html and php files that make up the main and subdomain sites.

To be clear, I want the foo.domain.org to show in the browser URL, and if they link to an html or php file from the index.html, I want to be able to see and move around all relative links, just like a "real" domain.

Problem:
I have tried two approaches to this using the .htaccess file, both "work" but I have the same problem with both - I see the initial index.html file in my browser, but when I click on relative links to files like test.php or anotherfile.php I get "Oops! This link appears broken. HTTP 404 - File not found."  At first I thought the .htacces was not working, but as I got into this, I can make html work fine, it is only the php not working. I also tried putting <?php echo "test"; ?> into the index.html file itself, but this never executes = does not generate html.
 
I have lots of items with subfolders, so I want to use wildcard subdomains and automagically make this work this way :-)

Here is my solution 1 and solution 2.  I appreciate any help you can give me on this. My brain is scrambled on .htaccess.

solution 1:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.org$ [NC]
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/folders/]+) [NC]
RewriteCond   %2<->%3 !^(.*)<->\1$ [NC]
RewriteRule   ^(.+) folders/%2/$1 [L]


solution 2:

RewriteEngine On
#fix trailing slashes
RewriteRule [^/]$ %{REQUEST_URI}/
#www is not a user subdomain
RewriteCond %{HTTP_HOST} ^www.domain.org [NC,OR]
RewriteCond %{HTTP_HOST} ^domain.org [NC]
RewriteRule ^(.*) $1 [L]
#redirect wildcard subdomains
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/folders/]+) [NC]
RewriteCond %{HTTP_HOST} ([^\.]+)\.domain\.org [NC]
# note the $1 is definitely index.html hardcoding to an index.html
RewriteRule ^(.*) /folders/%1$1 [PT]


Thanks!

jjltpcAsked:
Who is Participating?
 
caterham_wwwCommented:
I think we don't ned to make the regEx so complex here, because we can simply exclude the folder /folders/. We don't need to check for an empty HTTP_HOST, because we're checking for an existent one now in the 2nd "host condition".

[^/folders/]  does not exclude the string /folders/ but only the single characters '/deflors'.
RewriteEngine On
#fix trailing slashes
RewriteCond %{REQUEST_URI} !/$
RewriteRule !\.[a-z]{2,4}$ %{REQUEST_URI}/ [R=301,L]
 
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.org$ [NC]
RewriteCond $1 !^folders
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)
RewriteRule ^(.*) /folders/%2/$1 [L]

Open in new window

0
 
jjltpcAuthor Commented:
More info - I just tested the case with a sub folder inside the folders directory, and I even have a failure of an .html file. So the rewrite stuff must be wrong and causing the php problem too?  Here is the setup - using solution 2 from above:

/folders/test1/index.html    - this file shows in teh browser OK when I use the    test1.domain.org

/folders/test1/sub/subfile1.html   - this file will not work when I navigate from test1.domaIn.org/sub/subfile1.html


The URL is http://test1.itsmyplanet.org/sub/subfile1.html

I can't seem to get any logging to work, but the 404 page has this in it:
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /sub/subfile1.html was not found on this server.</p>
</body></html>

The file does work if I do:
http://www.domain.org/folders/test1/sub/subfile1.html

What I need is someone to parse my htaccess code and tell me what crazy thing mistake I have made.

Thanks!

0
 
jjltpcAuthor Commented:
Thank you! The answer works perfectly. I was afraid the problem had something to do with php, but the simplified .htaccess file you provided did the trick. This was my first question here, and I am changing my trial membership to 2yr subscribed. Great job!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.