I have been having some trouble getting the following to work:
<IfModule mod_rewrite.c>
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
# If the file is not found in web
RewriteCond web/$1 -f [NC] #<-- This line seems to be the problem.
RewriteRule ^(.*)$ web/$1 [L]
# Then rewrite it to index.php
RewriteRule ^(.*)$ index.php [L,QSA]
</IfModule>
The idea is to check if the file exists in the web directory, and if it doesn’t, route the request to index.php.
The problematic line above seems to pick up the correct URL, but it does not recognize that the file exists.
For example:
http://localhost/img.gif --> /www/web/img.gif
http://localhost/subdir/file.doc --> /www/web/subdir/file.doc
http://localhost/user/add --> /www/index.php
http://localhost/invalidurl --> /www/index.php
However, I cannot get static resources to be served correctly, they are all routed to index.php.
I also want to keep all URLs relative; so I can make this code reusable without editing it.
The following .htaccess gives an Internal Server Error, if I visit img.gif:
<IfModule mod_rewrite.c>
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
# If the file is not found in web
#RewriteCond web/$1 -f [NC] #<-- This line seems to be the problem.
RewriteRule ^(.*)$ web/$1 [L]
# Then rewrite it to index.php
#RewriteRule ^(.*)$ index.php [L,QSA]
</IfModule>
This .htaccess redirects to http://localhost/C:/absolute/path/to/web/img.gif, when visiting img.gif:
<IfModule mod_rewrite.c>
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
# If the file is not found in web
#RewriteCond web/$1 -f [NC] #<-- This line seems to be the problem.
RewriteRule ^(.*)$ web/$1 [R]
# Then rewrite it to index.php
#RewriteRule ^(.*)$ index.php [L,QSA]
</IfModule>
My conclusion from this would be that it’s getting the path correct, but some weirdness is causing it to do something completely weird (I don’t even know why it has an internal server error – Should be 404 not found).
Ok I got it:
When you do a rewrite, then keep in mind that the rewritten URL is called internally.
So you basically have to reprocess the .htaccess with mostly different values.
So in your example :
http://localhost/img.gifis directed tohttp://localhost/web/img.gifand then tohttp://localhost/index.phpby the last rule.I would try this (replace the last rule by: )
(NB: [QSA] is not needed since you don’t touch the query string, so it is passed as is.)
Edit : What about