htaccess Rewriterule if url contains a specific word, keep the rest of URL

血红的双手。 提交于 2020-01-05 17:00:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!