Francesco Boffa
Francesco Boffa

Reputation: 1402

.htaccess RewriteRule if exists in subdirectory

I had some experience with .htaccess and the mod_rewrite but I can't get this to work. Basically I wan't all URLs redirected to index.php EXCEPT those who point an existing files in the "public" subdirectory.

For example, if I have a file like /public/logo.png I want this:

http://example.com/logo.png -> /public/logo.png http://example.com/logo2.png -> index.php (logo2.png does not exist) http://example.com/whatever -> index.php

Thank you :)

Upvotes: 0

Views: 800

Answers (2)

Amine Hajyoussef
Amine Hajyoussef

Reputation: 4430

RewriteEngine on

RewriteCond  %{DOCUMENT_ROOT}/public/$1 -f
RewriteRule  ^(.*)$  /public/$1  [L]

RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^(.*)$ /index.php [R=301,L]

Upvotes: 2

Francesco Boffa
Francesco Boffa

Reputation: 1402

Thanks to Amine help I finally solved this, even though this looks quite messy:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ - [L]

RewriteCond  %{DOCUMENT_ROOT}/public/$1 -f
RewriteRule  ^(.*)$  /public/$1  [L]

RewriteRule ^(.*)$ /index.php [L]

Hope it helps

Upvotes: 2

Related Questions