php mod rewrite .htaccess ignore directory

2019-08-30 09:46发布

What rule should i set, to make the mod_rewrite ignore the directory "public" completely? By that, I mean, the files should be accessible within it, but if the file does not exist, a server error page should come up with something like, FORBIDDEN, or FILE NOT FOUND what ever. I do not need custom error pages or stuff like that. I simply want the "public" to behave like there is no mod_rewrite at all.

Here is my .htaccess file:

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

My file structure is

/system/
/application/
/public/

I want the folder public to behave, like there are no rewrite rules set at all, completely ignore it.

edit

That's my .htaccess file:

RewriteEngine on

RewriteRule ^(public)($|/) - [L,NC]

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

I already had this .htaccess in the /public/ folder:

RewriteEngine off

I've tried all the different answers above (and a ton from google). I've tried to mix 'em up what so ever.

My folders:

/system/
/application/
/public/ 
/public/.htaccess #RewriteEngine off
/public/favicon.ico
/index.php

Below are the url with results I'm getting:

/public/favicon.ico -> I get the favicon
/public/faviDon.ico -> I get the index.php (without mod rewrite you would get "not found")
/public/ -> I get the index.php (without mod rewrite "forbidden")

So it still does rewrite urls, if the file was not found, or upon accessing a folder directly.

Can you se it?

Thank you very much for effort guys! I really appreciate it!

1条回答
Summer. ? 凉城
2楼-- · 2019-08-30 10:09

EDIT

I completely setup your files on my machine

//  /.htaccess

RewriteEngine on

RewriteRule ^(public)($|/) - [L,NC]

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

.htaccess in the public folder:

//  /public/.htaccess

Options -Indexes
RewriteEngine off

This disables rewriting like you wanted.

/public/                 -> 403 Forbidden
/public/favicon.ico      -> 200 File found
/public/not-existing.ext -> 404 File not found

Do you have a index.php in you public folder? Maybe you could remove that one..

What kind of machine your testing on?

I tested it on Linux + Apache 2 + PHP5.3

I can give you more support in the afternoon (my time +2 GMT)

EDIT 2

When I remove this line from /.htaccess is still works

RewriteRule ^(public)($|/) - [L,NC]

Everything is handled by the .htaccess in the public folder.

Maybe it's a caching problem in your browser. Try a different browser/clean up history/install app to remove cache.. (depending on what browser you're using)

查看更多
登录 后发表回答