Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this htaccess end in a redirection loop?

I got the following htaccess that ends up in a redirection loop:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/int(.*)$
RewriteRule ^(.*)$ /int [R,L]

RewriteRule . index.php [L]

I added the first rule to redirect root requests to a specific sub folder. The second rule comes from WordPress (among others)

I dont see why this should end in a redirection loop. Does the second rewrite trigger another request so that the htaccess is checked again? Thant would explain the loop.

Thank you

like image 517
mario.schlipf Avatar asked Feb 21 '26 17:02

mario.schlipf


1 Answers

Each of your rewrites creates a new request which is reprocessed through the .htaccess. You're rewriting anything doesn't start with /int(.*) to /int. However, anything that starts with /int skips the first rewrite condition and gets rewritten to index.php. Then index.php gets rewriten to /int because it doesn't start with /int(.*). Then /int gets rewritten to index.php... and on ...and on...

BTW: You could prevent the endless loop by adding a second rewrite condition to not trigger your first rewrite rule to when the REQUEST_URI is /index.php

RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !^/int(.*)$
RewriteRule ^(.*)$ /int [R,L]

If forget if you need the leading forward slash in the first rewite condition... try it both ways.

like image 95
Ray Avatar answered Feb 23 '26 12:02

Ray