user1988588
user1988588

Reputation: 23

Redirecting all requests to index.php except /files subdirectory

I have very little understanding of .htaccess files so far. I want to redirect all requests to one index.php file in my root folder, except if the url links to a file that exists and is in the files folder. (e.g. example.com/files/picture.png) I want to achieve this with only htaccess file in the root directory and if necessary one in the files directory. Moreover, i don'd want to show a permission denied message.

Right now i have this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

But it does not redirect one to the index.php file if the file the url refers to exists. Any help? I sought on the internet but could not find anything.

Upvotes: 2

Views: 3945

Answers (1)

user1881928
user1881928

Reputation: 616

Something like this:

RewriteEngine on

RewriteCond %{REQUEST_URI}  /img/.+(gif|png|jpg)$
RewriteRule .* - [L]

RewriteCond %{REQUEST_URI}  /css/.+(css|png)$
RewriteRule .* - [L]

RewriteCond %{REQUEST_URI}  /js/.+css$
RewriteRule .* - [L]

RewriteCond %{REQUEST_URI}  /js/.+(js|htm)$
RewriteRule .* - [L]

RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

All requests will go through the index.php file except for the css javascript and image files. This .htaccess file i used for the codeigniter framework, so it can be a little bit different for your situation.

Upvotes: 5

Related Questions