Home > Net >  .htaccess rule to redirect all requests to a single php file, except 2 folders
.htaccess rule to redirect all requests to a single php file, except 2 folders

Time:04-15

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?

CodePudding user response:

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]
  • Related