Reputation: 488
I have a file structure like this:
root
|- public
|- file.jpeg
|- file.txt
|- .htaccess
|- index.php
Now, I want to rewrite example.com/file.jpeg
to example.com/public/file.jpeg
and the same for file.txt and every other file inside the public
directory, but if the file doesn't exists in the subdirectory then rewrite to index.php
For some reason this .htaccess rewrites everything to index.php
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
RewriteRule ^(.*)$ public/$1 [L]
RewriteRule ^(.*)$ index.php [L]
What is wrong with my code?
Upvotes: 1
Views: 243
Reputation: 785481
You're missing RewriteCond
to check for existing files/directories from last rule thus routing everything to index.php
.
You can use:
RewriteEngine On
RewriteBase /
# if direct access to /public needs be directed to index.php
RewriteCond %{THE_REQUEST} \s/+public [NC]
RewriteRule ^ index.php [L]
RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
RewriteRule ^(.*)$ public/$1 [L]
# if it is not /public/ then route to index.php
RewriteRule !^public/ index.php [L,NC]
Upvotes: 1