1

I need a good ".htaccess" rule to redirect all apache requests to a single php file, except for a 'files' folder (for js, css, etc) and a 'static' folder (for static .html files, like error pages).

Because this is a dev site, it's all in a subfolder right now. This is why I'm using 'SubFolder'.

I currently have the following:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(SubFolder/files|SubFolder/static)(/|$)
RewriteRule ^(.*)$ /SubFolder/index.php?path=$1 [NC,L,QSA]

When I access a file I get an "Internal Server Error". When I check the apache log it reports: "Request exceeded the limit of 10 internal redirects due to probable configuration error."

I suspect this is due to my rule catching itself repeatedly, but I can't see why. Can you help?

Kilbe3
  • 13
  • 2
  • Does this answer your question? [htaccess redirect all requestes to index.php except sub folder](https://stackoverflow.com/questions/13964027/htaccess-redirect-all-requestes-to-index-php-except-sub-folder) – adampweb Apr 13 '22 at 15:26
  • @AdamP. No, that answer has exceptions for existing files, but I don't want those exceptions. Therefore, it also doesn't have an exception for the script that should always be called. Thanks anyway! – Kilbe3 Apr 15 '22 at 06:47

1 Answers1

1

I suspect this is due to my rule catching itself repeatedly, but I can't see why.

Yes, your rule also matches /SubFolder/index.php so rewrites itself repeatedly. You can include an exception for this as well. For example:

RewriteCond %{REQUEST_URI} !^/SubFolder/index\.php$
RewriteCond %{REQUEST_URI} !^/(SubFolder/files|SubFolder/static)(/|$)
RewriteRule (.*) /SubFolder/index.php?path=$1 [L,QSA]

If this is the only rule you have then the RewriteBase directive is not required. (The NC flag is not required on this rule either, since .* matches both upper and lowercase letters anyway.)

You can combine the conditions if you want:

RewriteCond %{REQUEST_URI} !^/SubFolder/(index\.php|files|static)(/|$)
RewriteRule (.*) /SubFolder/index.php?path=$1 [L,QSA]
MrWhite
  • 43,179
  • 8
  • 60
  • 84