curl・HTTP確認コピペ集(ステータス200確認・ヘッダー・robots.txt・HTML取得・リダイレクト確認・PowerShellのcurl注意)
要点
Windows PowerShell 5.1 では curl が Invoke-WebRequest の別名なので、本物の curl は curl.exe と書く。ステータスは curl.exe -s -o NUL -w "%{http_code}"、ヘッダーは -I、リダイレクトは -IL で追う。すべて読み取りだけ。
PowerShell 5.1 の注意: curl と打つと Invoke-WebRequest の別名になり、オプションが違う。本物の curl を使うときは curl.exe と書く(Windows 10 以降は標準で入っている)。Git Bash では curl のままでよい。
ステータスコードだけ
curl.exe -s -o NUL -w "%{http_code}`n" https://example.com/
curl -s -o /dev/null -w "%{http_code}\n" https://example.com/ # Git Bash
複数URLをまとめて確認
Get-Content .\urls.txt -Encoding UTF8 | ForEach-Object {
$code = curl.exe -s -o NUL -w "%{http_code}" $_
"$code`t$_"
}
200 以外の行だけ見るなら末尾に | Where-Object { $_ -notmatch '^200' }。
ヘッダー
curl.exe -sI https://example.com/
curl.exe -sI https://example.com/ | Select-String -Pattern "x-robots-tag|cache|location|content-type"
リダイレクトを追う
curl.exe -sIL https://example.com/old/ | Select-String -Pattern "^HTTP|^location"
301 か 302 か、最終的に200になるか、ループしていないか(HTTPステータスとリダイレクトの基本(200・301・302・404・410・403・500・503))。
robots.txt・サイトマップ
curl.exe -s https://example.com/robots.txt
curl.exe -s -o NUL -w "%{http_code}`n" https://example.com/sitemap_index.xml
ページのHTMLを取得して確認
curl.exe -s https://example.com/ -o page.html
Select-String -Path .\page.html -Pattern '<meta name="robots"[^>]*>' -Encoding UTF8 # noindex の有無
(Select-String -Path .\page.html -Pattern '<h1' -Encoding UTF8).Count # H1の数
検索公開まわりの確認の全体は 検索公開まわりの確認方法(robots.txt・X-Robots-Tag・meta robots・canonical・サイトマップ)。
Invoke-WebRequest を使う場合
(Invoke-WebRequest https://example.com/ -UseBasicParsing -Method Head).StatusCode
404・500 だとエラー(例外)になって止まるので、一括確認には curl.exe の方が扱いやすい。
注意
- キャッシュの影響を避けたいときは URL に
?nocache=123などを付けて比べる(WordPressの変更が反映されない(キャッシュの種類と消し方))。 - 大量のURLを短時間に連続で叩かない(サーバー・WAFに負荷・ブロック)。
- ログイン後のページや認証付きURLの確認でCookie・トークンをコマンドに書かない(認証情報(パスワード・秘密鍵・APIキー・トークン)はknowledgeに保存しない)。