Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HT Access - Mod Rewrite

If i use:

RewriteEngine On
RewriteRule ^.* controller.php

It would send all requests to controller.php But if controller.php included a css file (/assets/css/main.css) then it wouldn't work, as when the browser called it, it would just redirect to controller.php

Is there a way i can fix this?

like image 421
tarnfeld Avatar asked Feb 10 '26 08:02

tarnfeld


1 Answers

You could add a condition to exclude URLs that can be mapped to actually existing files:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.* controller.php

The -f keyword will test if the absolute path in %{REQUEST_FILENAME} is a path to an existing regular file in the filesystem and !-f is just the inverse.

But if you have a fixed list of directories you want to exclude, you could also do this:

RewriteCond $0 !^(assets|foo|bar)/
RewriteRule ^.* controller.php

This condition tests if the match of the whole RewriteRule pattern (referenced with $0) does not begin with neither assets/ nor foo/ nor bar/. If you don’t want to process the match you could also use a negated expression directly in your RewriteRule directive:

RewriteRule !^(assets|foo|bar)/ controller.php
like image 105
Gumbo Avatar answered Feb 13 '26 08:02

Gumbo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!