I have a .htaccess file in my htdocs/MVC/ folder which looks like this
RewriteEngine On
# if its already been rewritten to the specific folder
RewriteCond %{REQUEST_URI} ^/MVC/public/img/(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^/MVC/public/index.php$
# skip next two rules
RewriteRule .? - [S=2]
# rewrite image requests
RewriteRule (.*\.(jpg|png|gif))$ public/img/$1 [L]
# rewrite all other requests to the front controller index.php
RewriteRule (.*) public/index.php?url=$1 [QSA,L]
What I want to achieve is all image requests going to the folder MVC/public/img all other requests going to the front controller index.php in the folder MVC/public. Is this the best way to set up those rules and prevent mod_rewrite to loop?
Using the
Sflag to skip is fine but the downside is:Other possible ways of preventing looping:
Putting this at the very top of your rules will instantly stop rewriting altogether if a rule was applied (or a mod_alias redirect was applied) in the previous loop.
So it’ll look like this:
Pros: You never have to worry about looping, because the 2nd time around instantly ends with the ENV check
Cons: If you ever happen to need to create a rule that loops, this method will prevent it from ever working