Vay Vay - 13 days ago 5
Apache Configuration Question

Rewrite Rules only work with specific User Agent?

I have a big problem that causes my whole web root to shut down (505 Internal Error) where every domain is affected, even if the htaccess file is only in one directory. My file structure is organized this way, so every domain is a seperate folder:

web root
-- [domainA]
(domainA files like the .htaccess file)
-- [domainB]
-- [domainC]
(web root files)


I want to implement my routing system and everything works fine on my localhost with this
.htaccess
code:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]


However, I'm testing for hours to make my routing work on my remote server with the same
.htaccess
code. I uploaded the file and it also works when I'm at the main directory like
www.domainA.com
.

BUT every link I'm clicking on my website like
www.domainA.com/signup
leads to an
505 Internal Server Error
. The whole domain isn't even responding for a long time. I have to delete the
.htaccess
file from my remote server and maybe after 15-30 minutes it's reachable again. That also makes this problem so difficult to debug. Also my whole web root seems to be affected since all other domains aren't reachable, too (even if the
.htaccess
file is only located in the seperate domain folder like in
[domainA]
).

Then I tried to apply my rule only on a specific user agent named
fake-user
:

RewriteCond %{HTTP_USER_AGENT} ^fake-user$
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]


This way it surprisingly works like on localhost and I don't get why. Obviously I don't want to limit my audience to user agents named
fake-user
to interact with my website. So currently all other users would "crash" my website. I hope to get some explanations what could cause this and how to fix this problem to make it work like on my localhost for all users?

Answer

Sounds like it could be recursion, try replacing your rule with this:

RewriteRule ^(?!index\.php$). index.php [NS,L]

which only rewrites when it's different.