Forum Moderators: phranque
i am really new to the whole htaccess/modrewrite thing so my experience with it and understanding of it are very limited...I am more than willing to invest the time in learning it, but I just need some assistance in getting started...and someone to tell me if what im asking for is even possible, and maybe lend me a hand in coding it...
so here's the background info...
i have been building a web application that will hopefully serve many different websites that will hopfully all be parked on top of one central application hosted on one domain, which i will refer to from here on as webapp.com...
the directory structure of webapp.com looks something like...
/index.php
/app/
/sites/
/sites/domain1.com/
/sites/domain1.com/images/
/sites/domain1.com/media/
/sites/domain2.com/
/sites/domain2.com/images/
/sites/domain2.com/files/
/sites/domain3.com/
etc...
so basically, the idea is that every website will be parked on top of this same folder, and the same webapp.com domain, and then stored in a database. I don't want to redistribute this web application all over the place because I want to keep one code base to maintain and build on...I don't want to support x number of installations of it since I will be building on it extensively...and I would like all the functionality to be available on all the sites using the system.
Inside the /app/ folder is a framework I am working on that will basically build out the websites by process templates, databases, etc...
so, hopefully thats a good enough description of the system...now on to what i want to do with .htaccess/mod_rewrite
I currently have a mod_rewrite that passes all of the parameters on the url to the index file which is used to interpret what the public user is asking for...
so for example: domain.com/test/ would redirect to domain.com/index.php?args=test which lets the web app know that the public user asked for the "test" page...
I need to keep this functionality. I would like to also extend the functionality so that if a person were to type in a path to a file, like domain1.com/images/image.jpg it would be masked and rewritten to domain1.com/sites/domain1.com/images/image.jpg
Basically, I want to store the unique files for each website with each respective domain's folder...so all images for domain1.com are stored in /sites/domain1.com/ and all files that are unique to domain2.com are stored in /sites/domain2.com/ and so on...
so, the rule that i see is that IF THERE IS A file extension of any kind, rewrite the url to point to that path except append /sites/domain/ to the beginning of it....
so again, a request for mydomain.com/picture.jpg would actually be rewritten as mydomain.com/sites/mydomain.com/picture.jpg
IF THERE IS NOT A FILE EXTENSION, rewrite the URL like it is currently doing. So basically...
domain.com/test/ rewrites to domain.com/index.php?args=test
&
domain.com/images/image.jpg rewrites to domain.com/sites/domain.com/images/image.jpg (has a file extension)
and another example would be...
domain.com/file.extension would rewrite to domain.com/sites/domain.com/file.extension (again has an extension)
so basically, if there is a file extension append the path on the end of /sites/domain.com/
If this doesn't make much sense, I will be more than happy to answer questions about my intentions....and again....
thank you for even reading this, and second, thank you so much if you decide to help me out in any way...even if its to tell me this is not possible...and im an idiot for even asking...
[edited by: jdMorgan at 4:54 am (utc) on Sep. 13, 2007]
[edit reason] De-linked [/edit]
The word "park" as pertains to a domain name is so imprecise as to mean nothing. What (I hope) you are doing is to "point" the various domain names to your server's IP address using DNS, and then you're configuring the server to "point" all requests for those domain names (now termed hostnames) to the same filespace (DocumentRoot in Apache-speak) on the server's disk.
Second, this sounds wrong (at a detail level):
so for example: http://example.com/test/ would redirect to http://example.com/index.php?args=test which lets the web app know that the public user asked for the "test" page...
In contrast, an internal rewrite just says, "If you get a request for this URL, then get the content from that file.
Risking the appearance of being pedantic, this information is meant to make things clearer for you and for those who might read or post answers here later.
A rule to internally rewrite extensionless URLs to /index.php with the URL-path as a query string, e.g. /test/ to index.php?args=test:
RewriteRule ^(([^/]+/)*[^.]+)/?$ /index.php?args=$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?([^:]+)(:[0-9]+)?$
RewriteRule ^(([^/]+/)*([^.]+)\.(.+))$ /sites/%2/$1 [L]
# Externally redirect to force "www" on arbitrary (but valid) domain names
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(([^.]+\.)+[^.:]+)(:[0-9]+)?$
RewriteRule (.*) http://www.%1/$1 [R=301,L]
# Externally redirect to remove "www" from arbitrary (but valid) domain names
RewriteCond %{HTTP_HOST} ^www\.(([^.]+\.)+[^.:]+)(:[0-9]+)?$
RewriteRule (.*) http://%1/$1 [R=301,L]
The code above makes many assumptions about the desired behaviour, and the customization of this code is left as an exercise for the reader. Crib notes here [httpd.apache.org]. ;)
For additional information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].
Jim
Sorry about that, I mistakenly wrote "redirect" when I meant "rewrite" but thank you for clarifying...sometimes those two things are misused with the same purpose in mind.
And thanks so much for actually writing me back, and taking the time to do that. So, the first thing I did was I took the condition and rule you wrote for the file extentions and copied it into a .htaccess file with mod_rewrite turned on, and then tested it by visiting domain1.com/file.jpg which is set up to point to the web application folder that has the .htaccess file within it. For some reason, I am getting an 500 Internal Server Error. After looking at the Apache log, I found this:
[Wed Sep 12 22:14:10 2007] [error] [client 127.0.0.1] mod_rewrite: maximum number of internal redirects reached. Assuming configuration error. Use 'RewriteOptions MaxRedirects' to increase the limit if neccessary.
So I am assuming it got stuck in some infinite loop of some sort? Any thoughts here?
[edited by: jdMorgan at 4:55 am (utc) on Sep. 13, 2007]
[edit reason] De-linked [/edit]
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?([^:]+)(:[0-9]+)?$
RewriteRule ^(([^/]+/)*([^.]+)\.(.+))$ /sites/%2/$1 [L]
</IfModule>
what could i be doing wrong?
i am requesting the url
http://example.com/images/pic.jpg
which should return http://example.com/sites/swamped.com/images/pic.jpg
correct?
[edited by: jdMorgan at 4:56 am (utc) on Sep. 13, 2007]
[edit reason] example.com [/edit]
Or rather, as is the actual case, what might Jim have forgotten? :)
RewriteRule ^(([^/]+/)*[^.]+)/?$ /index.php?args=$1 [L]
#
[i]RewriteCond $1 !^sites/[/i]
RewriteCond %{HTTP_HOST} ^(www\.)?([^:]+)(:[0-9]+)?$
RewriteRule ^(([^/]+/)*([^.]+)\.(.+))$ /sites/%2/$1 [L]
Jim
[edit] Fixed code as noted below. [/edit]
[edited by: jdMorgan at 5:15 am (utc) on Sep. 13, 2007]
RewriteRule ^(([^/]+/)*[^.]+)/?$ /index.php?args=$1 [L]
#
RewriteCond $1!^sites/
RewriteCond %{HTTP_HOST} ^(www\.)?([^:]+)(:[0-9]+)?$
RewriteRule ^(([^/]+/)*([^.]+)\.(.+))$ /sites/%2/$1 [L]
that seems to work
if i goto
[swamped.com...]
i get this error
"Not Found
The requested URL /sites/swamped.com/index.php was not found on this server."
and i am trying to figure out what this means, can't quite understand it...