2

I am trying to remove the Facebook tracking query strings from URLs--sort of like in this question:

What is fbclid? the new facebook parameter

I have a server I own (Apache 2.4) that does not allow .htaccess files for other reasons, so the directive for rewriting the URLs needs to be set in apache2.conf (I assume). Here is what I have:

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
    RewriteEngine On
    RewriteCond %{QUERY_STRING} "^(.*)&?fbclid=[^&]+&?(.)$" [NC]
    RewriteRule ^(.*)$ /$1?%1%2 [R=301,L]
</Directory>

This captures the ?fbclid= garbage successfully, but the problem is that it takes the link http://example.com/~userdir/stuff/?fbclid=... and forwards to http://example.com/userdir/public_html/stuff.

I assume I need to supply some sort of a variable in the RewriteRule line to make this work correctly, but I don't know what it should be.

Oh, also, not sure if this is relevant, but there is a example.conf configuration file in sites-available and sites-enabled, which specifies some trivial stuff in VirtualHost, like ServerName and ServerAlias both pointing to servername.com. I can provide further details if this is crucial.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
Yebeg Tibs
  • 23
  • 4

1 Answers1

1
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
    RewriteEngine On
    RewriteCond %{QUERY_STRING} "^(.*)&?fbclid=[^&]+&?(.)$" [NC]
    RewriteRule ^(.*)$ /$1?%1%2 [R=301,L]
</Directory>

there is a example.conf configuration file in sites-available and sites-enabled, which specifies some trivial stuff in VirtualHost, like ServerName and ServerAlias

You are certainly putting the directives in the wrong place. You should be editing the .conf file that relates to this domain and placing your directives inside the VirtualHost container that relates to your site (assuming you are using vHosts and potentially hosting multiple sites?).

You should not place your custom directives in the <Directory /> section in your main server config. This section relates to your entire server from the root filesystem directory. This section should only deny access (which it is doing with Require all denied) - you must already have a more specific <Directory> container elsewhere in your config (probably in the vHost container in the appropriate .conf file) that allows access, otherwise you site will simply not be accessible.

The problem you are seeing, in placing these directives in the "root" directory container (in a directory context) is that the captured URL-path is the absolute filesystem path - which is not what you require.

You need to revert this <Directory /> container back to what it was. Probably something like:

<Directory />
    AllowOverride None
    Require all denied
</Directory>

You then need to find the appropriate vHost container - probably in the <servername>.conf file you mentioned. You don't actually need to use a <Directory> container*1, but if you do then it needs to target your document root directory (ie. the root directory where your HTML files go).

(*1 Note, however, that the mod_rewrite directives being used here are tailored for a directory context.)

Your regex that captures the query string is not correct, as you are missing a * quantifier on the last capturing subpattern (looks like a copy/paste error?). You should also use a * quantifier if you want to remove empty fbclid= URL paramaters.

The surrounding quotes on the CondPattern are not required.

NB: First test with 302 (temporary) redirects to avoid potential caching issues and you will need to clear your browser cache before testing.

For example, using an appropriate <Directory> container in the relevant <VirtualHost>:

<Directory /absolute/file/system/path/to/document-root>
    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^(.*)&?fbclid=[^&]*&?(.*)$ [NC]
    RewriteRule (.*) /$1?%1%2 [R=301,L]
</Directory>

UPDATE: Alternatively, without using a <Directory> container try the following, directly in the <VirtualHost> or main server config instead:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)&?fbclid=[^&]*&?(.*)$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1%2 [R=301,L]

The REQUEST_URI server variable contains the root-relative URL-path.

Community
  • 1
  • 1
MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • Oh, I should have mentioned that I had tried modifying the domain `conf` file before I posted this, but it does not seem to have any effect when I put the Rewrite directives in the `.conf` VirtualHost container--whether I wrap them in `` or not. I only got the rewrite rules to do something when I modified `apache2.conf`. When I say rewrite rules have no effect, I mean that I go to a fbclid URL and it is not rewritten. It's not a cache issue, I've tried a fresh browser and I also can tell from the apache log also that the URL is getting visited. (Restarted apache, etc.) – Yebeg Tibs Dec 29 '19 at 19:40
  • p.s. the regex was actually fine in the .conf file but I typed it in wrong when posting the question! Thanks for the point about capturing empty `fbclid=` params, though. – Yebeg Tibs Dec 29 '19 at 19:43
  • The individual `.conf` files will need to be _included_ (via the `Include` directive) in the main `apache2.conf` file, if they are not already. Note that the directives as written are intended to be used in a `` container (ie. a _directory_ context). The directives will need to be changed if you want to put them directly in the vHost container (a _virtualhost_ context). mod_rewrite works differently in different _contexts_. – MrWhite Dec 29 '19 at 21:29
  • From your initial URL, it looks like you are using Apache's per-user web directories? If so, then you may not be using vHosts after all? Whether you are or you aren't the `` container still needs to target the filesystem document root being requested. If you are still seeing `public_html` in the resulting URL then it suggests this is not the case. – MrWhite Dec 29 '19 at 21:37
  • I've updated my answer with an alternative approach that does not require a `` container. – MrWhite Dec 29 '19 at 21:41
  • 1
    Aha--adding `%{REQUEST_URI}` to `apache2.conf` did it--I guess that was the "variable" I was searching for in the way I initially formulated my question. (I'll try playing with individual `.conf` files later; it does seem to be the way Apache 2.4 wants it). It wasn't super-clear to me from the Apache 2.4 docs how mod_rewrite interacted with the contexts; I think that part of the documentation is either really unclear or the information is not well organized. I spent all day yesterday reading it before I finally worked up the courage to post my first question here. Thanks for the help! – Yebeg Tibs Dec 30 '19 at 00:18