<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.