LoginSignup
1
2

More than 5 years have passed since last update.

さくらレンタルサーバーでWordpressのpostieプラグインをCRONで使う方法

Posted at

さくらレンタルサーバーのスタンダードプランで、httpsでWordpressを使っていますが、メール投稿のプラグインpostieをCRONで動かすことに、少しハマってしまったので、ここで書かせていただきます。

CRONに以下のように記述しましたが、メールを取得しませんでした。

/usr/local/bin/wget --spider --quiet --no-check-certificate "https://example.com/?postie=get-mail"

そこで、オプションを消して、

/usr/local/bin/wget "https://example.com/?postie=get-mail"

として、出力をpostmasterにメール受信してみました。
すると、以下のようになっていました。

--2018-02-19 19:20:00--  https://example.com/?postie=get-mail 
Resolving example.com... 219.94.xxx.xxx 
Connecting to example.com|219.94.xxx.xxx|:443... connected. 
HTTP request sent, awaiting response... 301 Moved Permanently 
Location: https://example.com/?postie=get-mail [following] 
--2018-02-19 19:20:01--  https://example.com/?postie=get-mail 
Reusing existing connection to example.com:443. 
HTTP request sent, awaiting response... 301 Moved Permanently 
Location: https://example.com/?postie=get-mail [following] 
--2018-02-19 19:20:01--  https://example.com/?postie=get-mail 
Reusing existing connection to example.com:443. 
HTTP request sent, awaiting response... 301 Moved Permanently 

301リダイレクトループしていました。
これは.htaccessで以下のようにhttpをhttpsにリダイレクトしているためでした。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{ENV:HTTPS} !^on$
RewriteCond %{HTTP:X-SAKURA-FORWARDED-FOR} ^$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

なので、自サーバーからのアクセスをリダイレクトから外すようにしました。
それがこちら↓

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^219\.94\.xxx\.xxx$
RewriteCond %{ENV:HTTPS} !^on$
RewriteCond %{HTTP:X-SAKURA-FORWARDED-FOR} ^$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

219\.94\.xxx\.xxxのところは、もちろんサーバーのIPにしてください。
これでCRONを実行したところ、postmasterへの出力メールが以下のようになりました。

--2018-02-19 19:30:00--  https://example.com/?postie=get-mail 
Resolving example.com... 219.94.xxx.xxx 
Connecting to example.com|219.94.xxx.xxx|:443... connected. 
HTTP request sent, awaiting response... 200 OK 
Length: unspecified [text/html] 
Saving to: 'index.html?postie=get-mail.3' 

     0K .......... .......                                     8.05K=2.1s 

2018-02-19 19:30:12 (8.05 KB/s) - 'index.html?postie=get-mail.3' saved [17581]

これできちんとメールを読み込んでWordpressに投稿がされました。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2