Link to home
Start Free TrialLog in
Avatar of Maarten Bruins
Maarten Bruins

asked on

RewriteRule causes an Internal Server Error in case of a subdirectory

On my domain, the .htaccess file looks like this:

RewriteRule ^ not-exists.php

Open in new window


A request for /just-something results in "Not Found". However, this:

RewriteRule ^ not-exists/not-exists.php

Open in new window


results in an Internal Server Error. Why? What is the difference? If both examples would give same result, then I would understand it, but now I don't understand it. So what iexactly is the difference between these two results?
Avatar of David Favor
David Favor
Flag of United States of America image

You have a malformed regular expression.

Change "^" to something like... maybe... "^(.*)$" so the "^" (anchor character) binds to something.

As you have it written, there's no anchor point.

Give this a try + drop a comment as to whether this works or not.

And... explain what you're trying to do also, because the rule you posted appears you're trying to route all page visits on a site (or maybe just one directory) to your not-exists.php file... which seems somewhat odd...
Avatar of Maarten Bruins
Maarten Bruins

ASKER

"^"  is a perfectly fine pattern, so you're wrong about that. It will do the same as for example: "^.*$" or "^(.*)$". That will not change the result.

And I'm trying to understand how it works. I don't need it in practise. I want to know why the result is like it is. It's just about the basics of RewriteRule. When I'm searching on the internet it's really hard to find the basics of RewriteRule. I need the basics that explains this example.
The answer to solving 500 errors is always to check your logs.

First check your Apache error.log + if to little detail is produced, enable deep logging of mod_rewrite + debug log data provided.
But my question is about how RewriteRule works. I understand the 500 error, but the question is why the first example doesn't give a 500 error.
For example:

RewriteRule ^ test.php?A=%{QUERY_STRING}

Open in new window


In the end, the query string is A=A=, so this means the .htaccess file has been run through 2 times. Then it stops, the result is NOT an Internal Server Error due to an endless loop. Apparently with the "Not Found" result, there is also no endless loop.

So how exactly Apache decides whether or not to stop encountering the .htaccess file? At least I would expect the same result in all three cases.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.