Score:1

Apache Rewrite Based on Query String

gb flag

I'm trying to detect the presence of a particular name/value pair in the query string and, if it exists, do a thing. I'm trying to do this in a conf file that's stored in conf-available/ and after a2enconf has been run to enable it. The code is small:

RewriteCond %{QUERY_STRING} foo=bar [NC]
RewriteRule ^(.*)$ http://linkedin.com [R=301,L]

No, redirecting to linkedin (or anywhere else) isn't my final goal, but I wanted to make it do something very obvious just to know that it's working (it's not, obviously). Just to verify that this conf is loaded and its code is being exercised, I added the following snippet right above:

RedirectMatch 301 ^(.*)$ http://www.cnn.com$1

The RedirectMatch works as expected. The RewriteRule does not.

Apache is running inside of a docker container and my local :8080 is forwarded to :80 on the container. I'm trying to access the site at http://localhost:8080?foo=bar. If I move this exact same code to a .htaccess file, it works exactly as expected.

Am I too far in the weeds here and missing something obvious?

UPDATE Full conf file below:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Verifies that this conf is loading properly
    # RedirectMatch 301 ^(.*)$ http://www.cnn.com$1

    RewriteCond %{QUERY_STRING} foo=bar [NC]
    RewriteRule ^(.*)$ http://linkedin.com [R=302,L]
</IfModule>
HBruijn avatar
in flag
When developing and testing redirect code, you might want to start with a different one than a 301 ***permanent*** redirect, as those should and will be cached by web browsers and after each change you'll need to test in a new incognito browser window. - Note that the default for the [`RewriteEngine`](https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriteengine) directive is `off`. When indeed your redirect config only consists of those two lines, not setting `RewriteEngine on` is a likely culprit.
gb flag
So turning rewrite on is the first bit of the conf file. I didn't mention that because it seemed obvious, but I should've done more to eliminate the obvious for you all that obviously aren't looking at the file. I'll update the original post accordingly. Thanks!
kz flag
You should certainly remove the `<IfModule mod_rewrite.c>` wrapper, since that is only going to mask any potential errors. Bear in mind that the `RedirectMatch 301` "verification" rule is going to be cached persistently by the browser. Is this rule in a _virtualhost_ context OR _directory_ context (ie. inside a `<Directory>` container)? Otherwise this should be "working"... we'll need to see your complete vHost config to debug this further.
gb flag
I can't mark it as the answer (yet), but I found my answer in [this question](https://stackoverflow.com/questions/344588/how-can-i-implement-a-global-rewritecond-rewriterule-in-apache-that-applies-to). The key info that I didn't know/have is that, "rewrite configurations are not inherited by virtual hosts. This means that you need to have a RewriteEngine on directive for each virtual host in which you wish to use rewrite rules."
kz flag
So, the config you've posted is not inside a vHost, but in the parent server config?
gb flag
That's correct. I thought the mention of using `a2enconf` would clarify that, but maybe there's more I don't know and that's not as disambiguous as I assumed.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.