Solved

htaccess rewriting wildcard subdomains to subfolders but php not working

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

777 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