# FB 分享連結的fbclid導致跳轉往頁失敗 20220119更新
-----
###### tags: `meta` `SEO` `webconfig` `html`
[網址自帶fbclid是什麼\?臉書第一方像素對GA\/短網址的影響](https://medium.com/picsee-official/facebook%E6%96%B0%E7%89%88%E5%83%8F%E7%B4%A010-24%E9%96%8B%E8%B7%91-%E8%87%AA%E5%B8%B6%E5%85%A5fbclid%E7%B6%B2%E5%9D%80%E5%8F%83%E6%95%B8%E7%9A%84%E5%9B%A0%E6%87%89%E5%BB%BA%E8%AD%B0-51c65b147f1)
## 20220119更新
網址 做得很像RESTful時
在分享時要注意
一般網址 首頁分成以下寫法(/index)
```
https://carrefour.show.bis.tw/2022_cnyluckbag
https://carrefour.show.bis.tw/2022_cnyluckbag/
```
寫法基本上是沒差
正常都會有定 預設文件 defaultDocument => index.php
所以其實上面網址都是指向
`https://carrefour.show.bis.tw/2022_cnyluckbag/index.php`
加上RESTful 格式(這三種正常都能看到網頁)
```
https://carrefour.show.bis.tw/2022_cnyluckbag/index
https://carrefour.show.bis.tw/2022_cnyluckbag/
https://carrefour.show.bis.tw/2022_cnyluckbag
```
但是FB分享
要注意尾巴不能加 **斜線**
**FB_Meta工具 又稱 FB_MetaDebug工具 要多刷幾次**
**注意 分享前一定要先用工具刷網址 不然先分享在刷網址圖片是不會變的**

**分享時 要注意圖片有沒有出來**

即使FB_MetaDebug 有出來圖片 但不建德分享時會有圖片

----
## 網址多fbclid webconfig無法解析
專案用webconfig去做網址導轉重寫
做得很像RESTful API
不過不算完全RESTful功能
而在將網站用FB分享時會被加上fbclid參數
而導致webconfig判斷失誤
一進到index時就會顯示alert(網址不正確)而踢出網頁

用web.config時
網址會變成 重要的id反而不見了
`https://carrefour.show.bis.tw/event_register/fbclid=xxxxxx`
原本
`https://carrefour.show.bis.tw/event_register/qqq`
## fbclid
fbclid就是fb在分析使用者判斷的參數
依照上述網頁
即使同一篇文章但fbclid也不同(後面太長刪了)
```
使用http
l.facebook.com/l.php?u=http%3A%2F%2Fcarrefour.show.bis.tw%2Fevent_register%2Fqqq%3F
使用https
l.facebook.com/l.php?u=https%3A%2F%2Fcarrefour.show.bis.tw%2Fevent_register%2Fqqq%3F
時間最早發布time
l.facebook.com/l.php?u=https%3A%2F%2Fcarrefour.show.bis.tw%2Fevent_register%2Fqqq%3F
1.
fbclid%3DIwAR38u90ofA7cov6w2x9ZL8h-EUQ8OQxTgLULnaoTgPqec9pqjh2sfNbGIm8&h=AT0ZnAGE5JVCk8v88SjG-zLODriyU059KWkjZgBw7r-FJaxRQ8oY8yVmkDok1kSAIiCl3y4gGR4pQLVMc2WN7-e2FdvM39Dqo8oe7qlTWCLk06Sk4k3Gb6RXCK3rWU3evsGN&__tn__=-UK-.......
2.
fbclid%3DIwAR1EwlfYHM4rF2-nOnZLk7C_mtEU4S8TUyOCBAtfzydkj55WRGXYSmxkNO0&h=AT0ZnAGE5JVCk8v88SjG-zLODriyU059KWkjZgBw7r-FJaxRQ8oY8yVmkDok1kSAIiCl3y4gGR4pQLVMc2WN7-e2FdvM39Dqo8oe7qlTWCLk06Sk4k3Gb6RXCK3rWU3evsGN&__tn__=-UK-.......
3.
fbclid%3DIwAR0DgetD63nXxcMONEkSBFSWJPyA_cJAF6bO6tjot54n_jl7jBtgTE-SqIM&h=AT0t2DI8nUVJhACCKuCwujIn1nLGzS4h9zg-POXan2K2txbI9IH0y-AFt24JIVkcc0QeebqJ1wNkPdsgBuxeavpDVFG-YNVegMRukr0Y3_Lqf6yWWq4uo1JLV2SweMiR9jXH&__tn__=-UK-.......
```
## 解決方式
這次的解決方式是用js先將後面參數清除在回到原來index頁
一開始連結會先去**fb的serve**
之後回來的網址會變成因為經由IIS轉換所以抓不到參數
`https://carrefour.show.bis.tw/event_register/fbclid=xxx`
所以在去index前先將所有url都抓起來
判斷有無fbclid字串
有字串的話就去fbReload.php(重寫網址不變)
這樣就完成fb分享連結跳轉不出錯了
```javascript=
<?php
?>
<script>
// ideally this is on top of page; works on bottom as well
// console.log(location.search);
if(/^\?fbclid=/.test(location.search)){
location.replace(location.href.replace(/\?fbclid.+/, ""));
}
</script>
```
## web.config
web.config
1. 第一個規則 就是清除fbclid參數
2. 第二個規則 就是所有url都要進來比對(除了檔案跟資料夾外)
```
<rules>
<rule name="test0" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="fbclid" />
</conditions>
<action type="Rewrite" url="fbReload.php" />
</rule>
<rule name="test1" enabled="true" patternSyntax="Wildcard">
<match url=".*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<serverVariables />
<action type="Rewrite" url="index.php" />
</rule>
<rule name="022" stopProcessing="true">
<match url="^(?!storeadmin(.*))" />
<conditions>
<add input="{URL}" pattern="event_register$" />
</conditions>
<action type="None" />
<!-- 有進來 -->
<!-- <action type="Redirect" url="https:www.google.com" /> -->
</rule>
<rule name="023" stopProcessing="true">
<match url="(/|)storeadmin(|/|/.*)" />
<action type="None" />
</rule>
<rule name="025" stopProcessing="true">
<match url="(.*)\.php" />
<conditions logicalGrouping="MatchAll">
<!-- <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> -->
<add input="{REQUEST_FILENAME}" pattern="(\.css|\.js|\.jpg|\.png|\.woff|\.woff(.*)|\.tiff|\.gif|\.dev|\.swf|captcha.php|index2.php)$" negate="true" />
<add input="{QUERY_STRING}" pattern="(|.*&)id=(.*)(&.*|)" />
</conditions>
<action type="Redirect" url="{C:2}" appendQueryString="false" />
</rule>
<rule name="026" stopProcessing="true">
<!-- <match url="(.*)" /> -->
<match url="(.*/|/.*|.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" pattern="(\.css|\.js|\.jpg|\.png|\.woff|\.woff(.*)|\.tiff|\.gif|\.dev|\.swf|captcha.php|index2.php)$" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?id={R:1}" appendQueryString="false" />
</rule>
</rules>
```
index.php
```php=
session_start();
// http 或是 https判斷
$protocol = ( (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443 ) ? "https://" : "http://";
// ip位置
$host = $_SERVER['HTTP_HOST'];
// 串網址(ex http://192.168.1.10)
$url = $protocol.$host;
$ecUrl = 'window.location.href="https://online.carrefour.com.tw/tw/?utm_source=ichannel&utm_medium=affiliates&gid=5a7e93b3f442e9011d106248333a35fc";';
$isStop = false; // true => JS關閉
$sql = "";
$setUrl = "";
$urlArrayList = array();
// 最新活動
$sql = "SELECT TOP(1) * FROM activitys WHERE del = '0' AND publish = '1' ORDER BY id DESC";
if (preg_match("/event_register\/(.*)/i", $_SERVER['REQUEST_URI'], $urlArrayList)) {
// 符合網址
$setUrl = preg_split('/&/', $urlArrayList[1], -1, PREG_SPLIT_NO_EMPTY);
$setUrl = $setUrl[0];
if ($urlArrayList[1] != '') {
$sql = "SELECT * FROM activitys WHERE del = '0' AND Publish = '1' AND setUrl = '" . $setUrl . "'";
}
}
DB_Connection();
$stmt = SQLselect($sql) or die("SQL發生問題!" . sqlsrv_errors());
// 活動筆數不是一筆
if (SQLcount($stmt) != 1) {
$isStop = true;
// 未清除fbclid這邊會出錯
echo '<script>alert("網址錯誤, 請聯絡家樂福"); window.location.href="https://www.carrefour.com.tw/"; </script>';
exit();
}
$rowActivity = sqlsrv_fetch_array($stmt);
DB_Close();
```
## FB Server
當我們分享網址在FB時
直接點下連結導入我們網站 只是網址後面多了一串fbclid
network上是看不出來有去過FB網站的

那要怎麼去確定有去過FB Server呢??
直接複製連結

貼上網址列後會發現網址開頭可是FB呢!!!

觀察network
先導去FB Server
回來時網址是有重新刷新過的
所以回來的網址列雖然有帶FB的參數 但已經是整理過的網址了

