Solved

htaccess rewriting wildcard subdomains to subfolders but php not working

Posted on 2008-09-30
3
3,643 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 500 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now