Reputation: 2952
OK, I'm pants at rewrite rules in .htaccess files!
My desired scenario is (using the URL http://doma.in/
as an example):
index.html
file exists in the /public
sub-dir; if it does, serve it
index.php
To expand on my example, say we requested the URL http://doma.in/js/foobar.js
:
foobar.js
file exists in the /public/js
sub-dir; if it does, serve it
index.php?controller=js%2Ffoobar.js
That would cover static files but I also need URLs like http://doma.in/foo
:
foo
file exists in the /public
sub-dir; if it does, serve it
index.php?controller=foo
And a URL http://doma.in/foo/bar
:
foo/bar
does not exists in the /public
sub-dir as files can't be named like that.
index.php?controller=foo&action=bar
I'm sure if this complicated (for me) bit is covered then I can work query-strings into the occasion too; so http://doma.in/foo/bar/foo/bar
would serve index.php?controller=foo&action=bar&querystring=foo%2Fbar
.
I'd also like to make sure that a trailing slash is handled the same as if a trailing slash was omitted, for example: http://doma.in/foo/bar/foo/bar
and http://doma.in/foo/bar/foo/bar/
I'll handle 404s from within my app as if the file did not exist, it would redirect to index.php
which does exist - I'm happy with this unless you've a better solution :)
I really hope all this makes sense as I've been looking to find a solution to this scenario all day now and this is all I have:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#RewriteBase /prompt-v3/
#RewriteCond %{REQUEST_URI} ^/prompt-v3/(.*)$
RewriteCond $1 !^public
RewriteRule ^(.*)$ public/$1 [R]
The commented-out lines deal with a sub-dir when on a remote host. So far I can redirect to the /public
sub-dir if the file exists there and that's about it.
Thank you everyone for your help!
Upvotes: 1
Views: 1280
Reputation: 2952
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#RewriteBase /sub-dir/
#RewriteCond %{REQUEST_URI} ^/sub-dir/(.*)$
RewriteRule ^index.html$ $1 [L,R=301]
RewriteCond $1 !^public
RewriteCond $1 !^lib
RewriteRule ^(.*)$ public/$1 [L]
RewriteCond $1 ^public/$
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteRule ^(.*)$ lib/bootstrap.php [L]
RewriteCond $1 !^public/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 ^public/(.*)$
RewriteRule ^(.*)$ lib/bootstrap.php?path=%1 [L]
This will look for an index.html file in the public directory and if it does not exist rewrite the URL to lib/bootstrap.php. it in fact checks for any request as a static file in the public directory first and deals with canonicalisation too.
Upvotes: 1
Reputation: 659
Put this .htaccess in your document root
<ifModule mod_rewrite.c>
RewriteEngine on
# For /js/foobar.js (/js/*)
RewriteRule ^js/(.*)$ /public/js/$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^public/(.*)$ index.php?controller=$1 [NC,L]
# For foo/bar (/*/*?*)
RewriteRule ^(.*)/(.*)$ /public/$1/$2 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^public/(.*)/(.*)$ index.php?controller=$1&action=$2&querystring=%{QUERY_STRING} [NC,L]
</IfModule>
Upvotes: 0