?
Solved

htaccess rewriting wildcard subdomains to subfolders but php not working

Posted on 2008-09-30
3
Medium Priority
?
3,666 Views
Last Modified: 2013-12-13
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!

0
Comment
Question by:jjltpc
  • 2
3 Comments
 

Author Comment

by:jjltpc
ID: 22610942
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
 
LVL 27

Accepted Solution

by:
caterham_www earned 2000 total points
ID: 22612501
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
 

Author Closing Comment

by:jjltpc
ID: 31501724
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question