Thank you one and all for your patience and help. I am completely restating the question because it is getting quite long from all my revisions. I have an PHP MVC framework with 4 entry points:

from the root:
index.php
index-ajax.php
admin/index.php
admin/index-ajax.php

I needed a .htcaccess file that would take any request and rewrite it to the corresponding file based on the url. The long url would be index.php?rt=cms/view/15 and I wanted it to be index/cms/view/15. That part is pretty much done except for one gotcha.

Here is my .htaccess file now:

# htaccess file for framework - GOOD
Options +FollowSymLinks

# Turn on the mod_rewrite engine - GOOD
RewriteEngine On

# Hide indexes - GOOD
Options -Indexes

# If a file is not one of these, continue processing. - GOOD
RewriteRule \.(css|js|jpg|jpeg|png|gif|ico)$ - [L]

# RewriteRules for folder index files
#RewriteRule ^(index)?(.php)?$ index.php [L] - GOOD
#RewriteRule ^admin(/?)(index)?(.php)?$ admin/index.php [L] - GOOD

# RewriteRules for admin folder arguements - going from more specific to less
RewriteRule ^admin/ajax/[A-Za-z0-9-_/]*$ admin/index-ajax.php?rt=$1 [L]
RewriteRule ^admin/[A-Za-z0-9-_/]*$ admin/index.php?rt=$1 [L]

# RewriteRule for root ajax file
RewriteRule ^ajax/[A-Za-z0-9-_/]*$ index-ajax.php?rt=$1 [L]

# RewriteRule for root file - by here, it is not ajax or admin related, so only
# possible option left if the root index file
RewriteRule ^[A-Za-z0-9-_/]*$ index.php?rt=$1 [L]

I have made a simple site with two folders - 'root' and 'root/admin', and inside of each of those a css,images, and javascript folder with some dummy content. There is an index.php and index-ajax.php file inside of 'root' and 'root/admin' that simple outputs whatever the url argument is and uses a css, js, and image file from each of the folders.

The problem I have now is that if I do a url like index/blah or /admin/index/blah, then the page presents right and the argument is right. However, when I do a url like index/blah/view or admin/index/blah/view then the arguement is right (?rt=blah/view) but the page presents wrong because the css/js/images file go to index/blah/[css] instead of index/[css].

Any ideas on how to handle this? I allowed css/js/image files to go through as is via the .htaccess so there would be less work there.

Accepted Answer

So the images/css/etc break because you're using a relative URL in the HTML, and now that you're going more nested they're breaking? With some other MVC setups I encountered, there's usually a "root path" variable of some sort, set by the Controller based on the request URL, and used by the View component to prefix all paths to assets like images/styles/scripts.

That turns your .htaccess problem into a Controller/PHP problem, which if that's an acceptable solution for you, would involve your Controller doing something like exploding the request URL on "/"s, counting the number of resulting pieces, and constructing a relative URL from the result.

I did a similar thing for a custom MVC setup I did; check out http://avogadro.ws/hosted/skel/public/results/school-1 and look at the head source for the CSS locations. The CSS for that page is actually at the http://avogadro.ws/hosted/skel/ level, which the Controller builds into a relative string.

This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki