OK, I’m pants at rewrite rules in .htaccess files!
My desired scenario is (using the URL http://doma.in/ as an example):
- First check to see if an
index.htmlfile exists in the/publicsub-dir; if it does, serve it- If it did not; serve (rewrite to)
index.php
- If it did not; serve (rewrite to)
To expand on my example, say we requested the URL http://doma.in/js/foobar.js:
- First check to see if an
foobar.jsfile exists in the/public/jssub-dir; if it does, serve it- If it did not; serve (rewrite to)
index.php?controller=js%2Ffoobar.js
- If it did not; serve (rewrite to)
That would cover static files but I also need URLs like http://doma.in/foo:
- First check to see if an
foofile exists in the/publicsub-dir; if it does, serve it- If it did not; serve (rewrite to)
index.php?controller=foo
- If it did not; serve (rewrite to)
And a URL http://doma.in/foo/bar:
- We can assume the file
foo/bardoes not exists in the/publicsub-dir as files can’t be named like that.- So serve (rewrite to)
index.php?controller=foo&action=bar
- So serve (rewrite to)
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!
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.