I am surprised that this generally works in context for each directory, since you would need to remove the path prefix for each directory from the template:
When using the rewrite mechanism in .htaccess files, the prefix for each directory (which is always the same for a specific directory) is automatically deleted to match the RewriteRule pattern and automatically added after any relative (not starting with a slash or protocol name) substitution meets the end of the ruleset .
In the case of the document root, which will be the leading / , the rule will look like this:
RewriteRule ^directory/(.*)\??(.*)$ /directory/?page=$1&$2
But also, the RewriteRule can only check the URI path, not the request; you need a RewriteCond for this. So try the following:
RewriteRule ^directory/(.+)$ /directory/?page=$1 [QSA]
Here .+ Instead of .* Infinite recursion should be avoided, since .* Matches everyone, even an empty line (this would be the case for an empty path segment after /directory/ ). And the QSA flag is to automatically add the requested URI request to the new one.
Gumbo source share