Forum Moderators: phranque

Message Too Old, No Replies

Parked domain htaccess solution?

I need help/direction in creating an htaccess file for a web application

         

blackpulp

1:49 am on Sep 13, 2007 (gmt 0)

10+ Year Member



hey there everyone, i am really hoping the web master community out there will have mercy on this little master here, and might lend me some "communal", "love thy neighbor", type help/expertise on this one...

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]

jdMorgan

2:43 am on Sep 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The requirements as stated are almost trivial. However, let's address two points -- One of them a pet peeve of mine...

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...

You almost certainly don't want an external redirect here, but rather an internal rewrite; Otherwise none of your search-engine-friendly URLs such as domain.com/test/ will be listed in search engines; They'll see the redirect and replace that static URL with example.com/index.php?args=test. A redirect informs the client, and actually requires the client's participation, because the client must re-request the redirected resource using the new URL given in the initial redirect response.

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]

Another rule to internally rewrite requests having file extensions to a domain-specific subdirectory after stripping the optional "www" subdomain and/or appended port number:

RewriteCond %{HTTP_HOST} ^(www\.)?([^:]+)(:[0-9]+)?$
RewriteRule ^(([^/]+/)*([^.]+)\.(.+))$ /sites/%2/$1 [L]

Finally, you may wish to canonicalize the domain names -- redirect to force them all to non-www or to the www subdomain:

# 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]

--or--

# 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 is intended to be used in the top-level .htaccess file. It can be modified for use in httpd.conf by adding a leading slash to the RewriteRule patterns; e.g. change "(.*)" to "^/(.*)$" or similar.

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

blackpulp

3:11 am on Sep 13, 2007 (gmt 0)

10+ Year Member



jd,

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]

blackpulp

4:39 am on Sep 13, 2007 (gmt 0)

10+ Year Member



So I have continued to play with this rule, but something about it must be causing an infinite loop. Here is my htaccess file:

<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]

jdMorgan

4:52 am on Sep 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> what could i be doing wrong?

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]

mod_rewrite in .htaccess is recursive, and loops must be explicitly prevented.

Jim

[edit] Fixed code as noted below. [/edit]

[edited by: jdMorgan at 5:15 am (utc) on Sep. 13, 2007]

blackpulp

4:58 am on Sep 13, 2007 (gmt 0)

10+ Year Member



again, maybe i am missing something, but this is still not working?

blackpulp

5:00 am on Sep 13, 2007 (gmt 0)

10+ Year Member



I think I might have got it, you had the "does not include" sites written as a rule, when it should have been a condition so....

RewriteRule ^(([^/]+/)*[^.]+)/?$ /index.php?args=$1 [L]
#
RewriteCond $1!^sites/
RewriteCond %{HTTP_HOST} ^(www\.)?([^:]+)(:[0-9]+)?$
RewriteRule ^(([^/]+/)*([^.]+)\.(.+))$ /sites/%2/$1 [L]

that seems to work

jdMorgan

5:14 am on Sep 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah, and I'm seriously reconsidering my habit of late-night postings... :)

Glad you got it working. I'll fix that coding error above so no one-else suffers with it -- sorry.

Jim

blackpulp

5:22 am on Sep 13, 2007 (gmt 0)

10+ Year Member



thanks for all your help...looks like there is another error...

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...

jdMorgan

5:27 am on Sep 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Doh!

It means another kind of unexpected redirect. The second rule is being invoked after the first rule writes a non-extension URL-path "blue" to one with an extension "index.php".

Add another exclusion to the second rule:


RewriteCond $1 !index\.php$

Too late for me...

Jim

blackpulp

2:48 pm on Sep 13, 2007 (gmt 0)

10+ Year Member



there was one more thing wrong that i found...and Im not sure why this effected it, but it did...when i changed the first rule to this, everything was groovy...

RewriteRule ^(([^/]*/)*[^.]+)$ index.php?args=$1 [L,QSA]

i basically took out the last / is was looking for before the end