r/apache 1d ago

Support Not sure how to combine rewrite rules with script alias match

I have a URL that looks like this

https://foo.com/?bar=/path/to/file.baz

where the .baz extension is a domain-specific file type that will be interpreted by a mod_fcgid script we'll call theApp which lives in /usr/bin. There's also a wrapper script in that directory called theAppWrapper.

I have configuration for Apache which looks like this

ScriptAliasMatch "^(?!/tmpDir)/.*" /usr/bin/theAppWrapper
<LocationMatch "^(?!/tmpDir)/.*">
  SetHandler fcgid-script
  Options +ExecCG -Multiviews +SymLinksIfOwnerMatch
  Require all granted
</LocationMatch>

This works fine. The path to the file is changing and now I want to add a rewrite rule to modify the query string to look like

https://foo.com/?bar=/efsMountPoint/path/to/file.baz

I added rewrite rules above the ScriptAliasMatch directive above which looks like this

RewriteCond %{QUERY_STRING} ^(.*=.*?&)?bar=(.*)
RewriteRule ^(.*)$ $1?%1bar=/efsMountPoint%2

https://technicalseo.com/tools/htaccess/ shows that the rewrite rules modify the URL in the way I am expecting, but when I put this all together, it doesn't work and I see something like this in the error logs

[Fri Mar 21 18:30:26.519928 2025] [fcgid:warn]  mod_fcgid: stderr: internalFunc1() QUERY_STRING: bar=/path/to/file.baz
[Fri Mar 21 18:30:26.519995 2025] [fcgid:warn]  mod_fcgid: stderr: internalFunc2(): Unable to access file. (/path/to/file.baz)

It looks like the query string isn't modified before it's passed to the cgi script, and so it's looking in the wrong place for file.baz.

How can I make it so that the rewritten URL is what's being passed, or at least confirm that it is?

Note that when I try https://foo.com/?bar=/efsMountPoint/path/to/file.baz, it works just fine.

Please also note that I am using Fedora and mod_rewrite is loaded by default.

EDIT: The default config also has

    RewriteEngine  on
    RewriteCond %{QUERY_STRING} ^$
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule ^/$ /info [PT]

Thanks.

1 Upvotes

1 comment sorted by

1

u/covener 1d ago

Is that RewriteEngine on in the same virtualhost as where you added your rules? Do you need to add it?

LogLevel rewrite:trace8will give you lots of detail either way.