简体   繁体   中英

.htaccess - Rewrite all except for 1 specific directory

I know this has been asked and answered already but I really don't get what I'm doing when using the code I see in the answers. Here's my issue:
My website uses a PHP router, it then doesn't send a specified file but generates a page according to the URI.
In order to get my router.php running for any URI, I created a simple .htaccess file, here it is:

 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule (.*) router.php [L] 

The thing is that I've got a directory containing all assets that a client may request (images, stylehseets, api...) titled "assets". I'd like to always get the router.php file, whatever the URI is, except if it asks for the assets directory.
Could anyone help me update my .htaccess code? Thank you! :)

It must be smth. like this:

# except /assets folder...
RewriteCond %{REQUEST_URI} !/assets
# if file, directory or symlink to file exists
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
# return that file
RewriteRule ^.*$ - [NC,L]

# else, any symlink and directory requests
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
# redirect to
RewriteRule ^.*$ router.php [NC,L]

# except of non-existent-file requests,
# that must bee feeded to some 404 handling script
RewriteRule ^.*\.[^\/\\]*$ 404.php [NC,L]

# all that left - non-existent-directory requests
RewriteRule ^.*$ router.php [NC,L]

Upd.

More strict config:

# in /assets folder...
RewriteCond %{REQUEST_URI} /assets
# if file exists
RewriteCond %{REQUEST_FILENAME} -s
# return that file
RewriteRule ^.*$ - [NC,L]

# else...
RewriteRule ^.*$ router.php [NC,L]

You just need a negative condition in your RewriteRule to allow specific directory or path:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^assets(/.*)?$ router.php [L,NC]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM