0

I have created a php framework that works well on an IIS server and the RewriteRules work perfectly well with web.config.

But the same rules don't work on an apache server with .htaccess and I don't understand why... here they are:

Options +FollowSymlinks

RewriteEngine On

RewriteRule picture/([a-zA-Z0-9]+)/([a-zA-Z\-0-9/]+).(jpg|png|gif)    app/Src/$1Bundle/public/img/$2.$3 [L]
RewriteRule picture/([a-zA-Z\-0-9/]+).(jpg|png|gif)    public/$1.$2 [L]
RewriteRule scripts/([a-zA-Z0-9]+)/(js|css|typo)/([a-zA-Z\-0-9=\./]+).(js|css) app/Src/$1Bundle/public/$2/$3.$4 [L]
RewriteRule scripts/(js|css|typo)/([a-zA-Z\-0-9=\./]+).(js|css) public/$1/$2.$3 [L]
RewriteRule scripts/(fonts)/([a-zA-Z\-0-9=\./]+).(eot|svg|ttf|woff)   public/$1/$2.$3 [L]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

I have tried this solution (and many others)(RewriteRule Last [L] flag not working?), but it hasn't worked.

RewriteCond %{ENV:REDIRECT_STATUS} != 200

I have run out of ideas...

EmmCall
  • 168
  • 1
  • 1
  • 13
  • 2
    What URLs are not working and what is the error? – anubhava Jul 09 '20 at 18:07
  • 2
    The usual issue with the `L` flag is that people do not understand that it only terminates _this_ run of the rewriting process. The process is however immediately started again if the URL has been rewritten. If you want to _finally_ terminate the rewriting process use the `END` flag instead. – arkascha Jul 09 '20 at 18:15

1 Answers1

1
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

Your last rule (that rewrites everything) runs unconditionally, so this is going to result in a rewrite loop (500 error response).

At the very least you need to prevent requests for index.php being further rewritten. For example:

RewriteRule ^index\.php$ - [L]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

But you presumably also want your static resources to remain directly accessible as well? So, excluding requests that map to physical files and directories is a common requirement. For example:

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
RewriteRule picture/([a-zA-Z0-9]+)/([a-zA-Z\-0-9/]+).(jpg|png|gif)    app/Src/$1Bundle/public/img/$2.$3 [L]

You have also omitted any start-of-string anchors (^) and end-of-string anchors ($) from your RewriteRule patterns. This could potentially result in matching too much - even matching the rewritten URL - another loop. You need to be more specific in your pattern matching.

MrWhite
  • 43,179
  • 8
  • 60
  • 84