解決Adsense ads.txt 抓不到問題,從Apache 設定修改

如果你的網址是 www.example.com,而你網站也有裝 Adsense 廣告,那麼 Adsense 要抓取你的 ads.txt,最好是 www.example.com/ads.txt 跟 example.com/ads.txt 這兩個地方都要能正確抓到,一般如果只是 www.example.com/ads.txt 抓得到而 example.com/ads.txt 無法正確顯示,那麼 Adsense 那邊還是會抓不到。

關於這個問題看從 Apache rewrite 中修改,前往。/etc/apache2/site-avalible,找到你的設定檔之後,加入以下語法:

<VirtualHost *:443> 
  ServerName example.com 
  ... # SSL Stuff 
  Redirect "/" "https://www.example.com/" 
</VirtualHost>

將上面 example.com 改成你的網址,然後在 Redirect 那一行設定你要的重定向語法。例如下面這樣:

#解決 ads.txt問題,將example.com 導向至www.example.com,並且將example.com/ads.txt導向至 example.com/ads.txt

<VirtualHost *:443>
  ServerName example.com
  Redirect "/ads.txt" "https://www.example.com/ads.txt"
  Redirect "/" "https://www.example.com"
</VirtualHost>

參考文章:

Apache 替網站加上 Secure Referrer-Policy Header

簡單的說,就是當網站有連結連到外部網站,referrer 要包含怎樣的資訊,有以下八種設定方式,每種包含的資訊都不一樣,差別可以參考文章參考連結。

Referrer-Policy: no-referrer
Referrer-Policy: no-referrer-when-downgrade
Referrer-Policy: origin
Referrer-Policy: origin-when-cross-origin
Referrer-Policy: same-origin
Referrer-Policy: strict-origin
Referrer-Policy: strict-origin-when-cross-origin
Referrer-Policy: unsafe-url

要在 Apache 伺服器上設置Secure Referrer-Policy Header,步驟如下。

#開啟 apache headers 模組
sudo a2enmod headers

#重新啟動 apache
sudo service apache2 restart

#/etc/apache2/conf-available/security.conf ,加上 header 設定
#Secure Referrer-Policy Header 
Header set Referrer-Policy: "no-referrer"


#重新開啟 apache 
sudo service apache2 restart

#成功設定,可以試著用瀏覽器看一下 response headers 有沒有多了  Referrer-Policy 

參考:Referrer-Policy

Apache 替網站加上 browser cache 的 Header

要在 Apache 伺服器上設置browser cache Header,步驟如下。

#開啟 apache headers 模組
sudo a2enmod headers

#重新啟動 apache
sudo service apache2 restart

#/etc/apache2/conf-available/security.conf ,加上 header 設定
# browser cache
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
  Header set Cache-Control "max-age=604800, public"
</FilesMatch>


#重新開啟 apache 
sudo service apache2 restart

#成功設定,可以試著用瀏覽器看一下 response headers 有沒有多了 cache control

參考:
HTTP Caching
Cache-Control

Apache 設定 X-frame Header

X-Frame-Options HTTP 回應標頭 (header) 用來指示文件是否能夠載入 (en-US), (en-US) 以及 <object> (en-US),網站可以利用 X-Frame-Options 來確保本身內容不會遭惡意嵌入道其他網站、避免 clickjacking 攻擊。

X-Frame-OptionsHeader 相關語法參考:X-Frame-Options 回應標頭

Header always append X-Frame-Options SAMEORIGIN

要在 Apache 伺服器上設置 X-Frame-Options Header,步驟如下。

#開啟 apache headers 模組
sudo a2enmod headers

#重新啟動 apache
sudo service apache2 restart

#/etc/apache2/conf-available/security.conf ,加上 header 設定
Header always append X-Frame-Options SAMEORIGIN


#重新開啟 apache 
sudo service apache2 restart

#成功設定,可以試著用瀏覽器看一下 response headers 有沒有多了X-Frame-Options SAMEORIGIN

參考:X-Frame-Options 回應標頭

Apache 設定 HSTS Header

HSTS Header 主要是告訴瀏覽器,這個頁面要用 HTTPS 來連結,這是網站安全性的問題。

HTTP Strict-Transport-Security 回應標頭(簡稱為 HSTS (en-US))告知瀏覽器應強制使用HTTPS以取代HTTP。

HSTS Header 相關語法參考:Strict-Transport-Security

Strict-Transport-Security: max-age=<expire-time>
Strict-Transport-Security: max-age=<expire-time>; includeSubDomains
Strict-Transport-Security: max-age=<expire-time>; preload

要在 Apache 伺服器上設置 HSTS Header,步驟如下。

#開啟 apache headers 模組
sudo a2enmod headers

#重新啟動 apache
sudo service apache2 restart

#/etc/apache2/conf-available/security.conf ,加上 header 設定
Header always set Strict-Transport-Security "max-age=31536000;includeSubdomains; preload"

#重新開啟 apache 
sudo service apache2 restart

#成功設定,可以試著用瀏覽器看一下 response headers 有沒有多了 Strict-Transport-Security: max-age=31536000;includeSubdomains; preload

參考:
apache 設定 HSTS ( HTTP Strict Transport Security )

Ubuntu 20.0 解決mysql binlog 檔案越來越多問題

mysql 的 binglog 檔案會越來越多越來越大,可以輸入以下指令設定 binlog 保留天數。

sudo mysql -u root -p

進入mysql 之後輸入以下

下面這行指令可以讓你查看 bin_log 是否是開啟狀態。

SHOW VARIABLES LIKE 'log_bin';

如果log_bing 是開啟的,你的伺服器就會有越來越多binlog 檔案,這樣至少要設定一下天數,避免檔案過大。

輸入下面這行指令檢查一下:

show variables like 'binlog_expire_logs_seconds';

如果你看到是 empty,也就是0 的狀態,代表沒有設置時間,也就是無保留天數,這時你可以設定成 259200 ,也就是3天。

輸入以下:

set global binlog_expire_logs_seconds=259200;
flush logs;
#記得 flush logs 一下。完成上面兩個指令之後,binglog 就會變成只保留三天了。

這時候再次輸入以下:

show variables like 'binlog_expire_logs_seconds';

這樣就可以看到結果了。259200 = 保留三天 bin_log。

參考:

【MYSQL】刪除binlog的方法

Working with MySQL Binary Logs

The Binary Log

WordPress:將文章的分類名稱,製作成 shortcode

要用 shortcode 顯示文章分類名稱,可以在 function.php 加入以下程式碼:

function shortcode_post_category($atts=array()) {
	// get taxonomy by post type
	$tax = get_object_taxonomies(get_post_type());

	// get the categories of the taxonomy
	$cats = get_the_terms($post->ID, $tax[0]);

	$result = '';
	foreach ($cats as $cat) {
		$result = $cat->name;
		return $result;
	}
	return $result;
}
add_shortcode('post_category', 'shortcode_post_category');


參考文章:

How To: Get Single Post Category With a WordPress Shortcode

WordPress :AIOSEO 外掛不要輸出 breadcrumbList Schema Data

AIOSEO 預設都會輸出 breadcrumbList 這個麵包屑 Schema Data,但輸出的麵包屑又是錯誤的,很討厭。

add_filter( 'aioseo_schema_output', 'aioseo_filter_schema_output' );

function aioseo_filter_schema_output( $graphs ) {
foreach ( $graphs as $index => $graph ) {
if ( 'BreadcrumbList' === $graph['@type'] ) {
unset( $graphs[ $index ] );
}

foreach ( $graph as $key => $value ) {
if ( 'breadcrumb' === $key ) {
unset( $graphs[ $index ][ $key ] );
}
}
}
return $graphs;
}

在function.php 加入上面程式碼,就可以禁止 AIOSEO 輸出 breadcrumbList Schema Data 了。

參考:

How to disable Breadcrumbs in aioseo-schema