问题
Facebook recently added a tracking script/parameter: &fbclid=.... So when people click on a link from a Facebook page which refers to my website (portfolio), my website can't find the right page.
An example:
This is the url facebook creates: http://stanbroeksteeg.nl/portfolio?video=296818340&fbclid=IwAR1OuXwzLVBQDFSgeNzCVgJ7lkCNhWeHZwhOe2vs9SmZzCYFMcBDJ6N-lX8
My website quickly changes it to:
http://stanbroeksteeg.nl/portfolio?video=296818340&fbclid
This results in the page not being found. So what i want to do is the following:
If the url contains &fbclid remove this part and KEEP the rest of the URL. So if i take the example URL is needs to be like this:
http://stanbroeksteeg.nl/portfolio?video=296818340
I've come up with this these Rewrite rules:
RewriteCond %{QUERY_STRING} “fbclid=” [NC]
RewriteRule (.*) /$1? [R=301,L]
or
RewriteCond %{QUERY_STRING} ^(.*)&?fbclid=(.*)$ [NC]
RewriteRule ^/?(.*)$ /$1?%1 [R=301,L]
But both rules do not resolve the issue. Can somebody help me with this?
This is my current .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [QSA]
SetOutputFilter DEFLATE
<FilesMatch ".(eot|ttf|otf|woff|woff2)">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
</IfModule>
回答1:
I found a solution for my problem
RewriteCond %{QUERY_STRING} ^(.*)&?fbclid=[^&]+&?(.*)$ [NC]
RewriteRule ^(.*)$ /$1?%1%2 [R=301,L]
Hope it can help others in the future
回答2:
@Bram's solution worked in my Wordpress. Here is the .htaccess code before and after.
Before:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
After:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)&?fbclid=[^&]+&?(.*)$ [NC]
RewriteRule ^(.*)$ /$1?%1%2 [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
来源:https://stackoverflow.com/questions/52984196/htaccess-rewriterule-if-url-contains-a-specific-word-keep-the-rest-of-url