【问题标题】:htaccess Rewriterule if url contains a specific word, keep the rest of URLhtaccess Rewriterule 如果 url 包含特定的单词,则保留 URL 的其余部分
【发布时间】:2018-10-25 07:58:11
【问题描述】:

Facebook 最近添加了一个跟踪脚本/参数:&fbclid=.... 因此,当人们单击 Facebook 页面上指向我的网站(投资组合)的链接时,我的网站找不到正确的页面。

一个例子:
这是 facebook 创建的网址:http://stanbroeksteeg.nl/portfolio?video=296818340&fbclid=IwAR1OuXwzLVBQDFSgeNzCVgJ7lkCNhWeHZwhOe2vs9SmZzCYFMcBDJ6N-lX8

我的网站很快将其更改为:
http://stanbroeksteeg.nl/portfolio?video=296818340&fbclid

这会导致找不到页面。所以我想做的是:
如果 url 包含 &fbclid 删除这部分并保留 URL 的其余部分。因此,如果我以示例 URL 为例,则需要是这样的:
http://stanbroeksteeg.nl/portfolio?video=296818340

我想出了这些重写规则:

RewriteCond %{QUERY_STRING} “fbclid=” [NC]
RewriteRule (.*) /$1? [R=301,L]

RewriteCond %{QUERY_STRING} ^(.*)&?fbclid=(.*)$ [NC]
RewriteRule ^/?(.*)$ /$1?%1 [R=301,L]

但是这两个规则都不能解决问题。 有人可以帮我解决这个问题吗?

这是我当前的 .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>

【问题讨论】:

    标签: url-rewriting


    【解决方案1】:

    我找到了解决问题的方法

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

    希望以后能帮到别人

    【讨论】:

      【解决方案2】:

      @Bram 的解决方案在我的 Wordpress 中有效。这是之前和之后的.htaccess代码。

      之前:

      # 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
      

      之后:

      # 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-21
        • 1970-01-01
        • 1970-01-01
        • 2016-03-23
        • 1970-01-01
        • 2011-05-03
        相关资源
        最近更新 更多