Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

htaccess rewriting wildcard subdomains to subfolders but php not working

Posted on 2008-09-30
3
Medium Priority
?
3,660 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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

610 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