tag:crieit.net,2005:https://crieit.net/tags/.htaccess/feed 「.htaccess」の記事 - Crieit Crieitでタグ「.htaccess」に投稿された最近の記事 2021-11-24T23:44:18+09:00 https://crieit.net/tags/.htaccess/feed tag:crieit.net,2005:PublicArticle/17781 2021-11-24T23:44:18+09:00 2021-11-24T23:44:18+09:00 https://crieit.net/posts/php-fpm-php-value-error-20211124 PHP-FPM 環境で .htaccess に php_value を使用したら 500 Internal Server Error <p>表題の通り、 PHP-FPM 環境で <code>.htaccess</code> に <code>php_value</code> を使用したら 500 Internal Server Error. が発生したので対処をメモしておきます。</p> <h2 id="現象"><a href="#%E7%8F%BE%E8%B1%A1">現象</a></h2> <pre><code class="htaccess">php_value memory_limit 64M php_value post_max_size 32M php_value upload_max_filesize 16M </code></pre> <p><code>.htaccess</code> にこのようなファイルアップロード関係の設定を記述しておくと、 500 Internal Server Error. が発生しました。</p> <p>なお、サーバログには次のように記録されていました。</p> <pre><code>/PATH/TO/WEBDIRECTORY/.htaccess: Invalid command 'php_value', perhaps misspelled or defined by a module not included in the server configuration, referer: https://example.com/ </code></pre> <h2 id="調査・対処"><a href="#%E8%AA%BF%E6%9F%BB%E3%83%BB%E5%AF%BE%E5%87%A6">調査・対処</a></h2> <p>このサーバログで検索すると次の記事がヒット。</p> <ul> <li><a target="_blank" rel="nofollow noopener" href="https://mattintosh.hatenablog.com/entry/20200605/1591359561">PHP-FPM 環境で .htaccess に php_value は書ける? - mattintosh note</a></li> </ul> <p>PHP-FPM 環境では <code>php_value</code> は使用できないとのこと。</p> <p>今回はこのケースに該当していたため、対処します。</p> <h3 id=".htaccess"><a href="#.htaccess">.htaccess</a></h3> <pre><code class="htaccess"># php_value memory_limit 64M # php_value post_max_size 32M # php_value upload_max_filesize 16M <Files ".user.ini"> Require all denied </Files> </code></pre> <p>まず <code>.htaccess</code> は <code>php_value</code> を止めて、代わりに次に記述する <code>.user.ini</code> へのWebアクセスを禁じる設定を追記します。</p> <h3 id=".user.ini"><a href="#.user.ini">.user.ini</a></h3> <p>続いて <code>.user.ini</code> に先程の <code>php_value</code> の設定と同様の内容を記述します。</p> <pre><code class="ini">memory_limit 64M post_max_size 32M upload_max_filesize 16M </code></pre> <p>これで動作確認し、問題ないことを確認しました。</p> <h2 id="参考"><a href="#%E5%8F%82%E8%80%83">参考</a></h2> <ul> <li><a target="_blank" rel="nofollow noopener" href="https://mattintosh.hatenablog.com/entry/20200605/1591359561">PHP-FPM 環境で .htaccess に php_value は書ける? - mattintosh note</a></li> </ul> arm-band tag:crieit.net,2005:PublicArticle/17762 2021-11-15T00:07:31+09:00 2021-11-15T00:07:31+09:00 https://crieit.net/posts/forbid-link-direct-image-in-wordpress-20211115 WordPress で画像の直リンクを禁止する <p>表題の通り、 WordPress で画像の直リンクを禁止する方法についてメモ。</p> <p>ページ内とかであれば WordPress の処理である程度どうにかできますが、直接参照されると WordPress としての処理は走らないので効かないのですよね……。</p> <h2 id="対処"><a href="#%E5%AF%BE%E5%87%A6">対処</a></h2> <pre><code class="htaccess"><IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_REFERER} !^http(s)?://example\.com [NC] RewriteRule \.(jpg|png|gif|wepb)$ - [F] </IfModule> </code></pre> <p>対処としては <code>.htaccess</code> を記述することで抑制できます。</p> <p><code>RewriteEngine on</code> でリダイレクトを有効にして、 <code>RewriteCond %{HTTP_REFERER} !^http(s)?://example\.com [NC]</code> (実際は <code>example.com</code> ではなく実際のサイトのドメイン) で自サイトは許可、それ以外は禁止とします。</p> <p>最後に <code>RewriteRule \.(jpg|png|gif|wepb)$ - [F]</code> で画像に該当するファイルの拡張子の場合は参照できなくしてしまう、と。</p> <p>これである程度は抑制できるのではないでしょうか。</p> <h2 id="参考"><a href="#%E5%8F%82%E8%80%83">参考</a></h2> <ul> <li><a target="_blank" rel="nofollow noopener" href="https://webllica.com/disable-direct-access-to-image-by-htaccess/">WordPress でアップロードした画像への直リンクを禁止する方法</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://mori.moripower.jp/eqwip/wordpress/1296.html">Wordpressの画像等の直リンクを禁止する ? GettingOUT</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://fukuro-press.com/direct-link-ban-with-htaccess/">画像などの直リンクを禁止してブログのパクリを防ぐ方法 | Fukuro Press</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://emmalanglab.com/prevent-hotlinking/">画像への直リンク防止策 - kotonoha</a></li> </ul> arm-band tag:crieit.net,2005:PublicArticle/17521 2021-07-18T17:02:26+09:00 2021-07-18T17:02:26+09:00 https://crieit.net/posts/change-output-php-log-by-htaccess-20210718 .htaccess で PHP のエラーログの出力先を変える <p><code>/var/log/</code> に潜らなくとも、あるいは <code>php.ini</code> を編集しなくとも PHP のログを手軽に簡単に確認できる場所に出力できないか試してみました。</p> <pre><code class="htaccess">php_flag log_errors On php_value error_log PATH/TO/LOG/error_log.log </code></pre> <p>結果、上述二文を <code>.htaccess</code> に記述することで PHP のログの出力先を変えることができました。</p> <h2 id="参考"><a href="#%E5%8F%82%E8%80%83">参考</a></h2> <ul> <li><a target="_blank" rel="nofollow noopener" href="https://www.welcart.com/documents/archives/3706">PHP のエラーログを出力するにはどうしたら良いですか? - Welcart サポート</a></li> </ul> <h3 id="PHPでどうにかしようとした (失敗)"><a href="#PHP%E3%81%A7%E3%81%A9%E3%81%86%E3%81%AB%E3%81%8B%E3%81%97%E3%82%88%E3%81%86%E3%81%A8%E3%81%97%E3%81%9F+%28%E5%A4%B1%E6%95%97%29">PHPでどうにかしようとした (失敗)</a></h3> <ul> <li><a target="_blank" rel="nofollow noopener" href="https://qiita.com/notona/items/b821ed0ad04ac3ef8f80">php のエラー発生時に処理をはさみたい - Qiita</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://ysklog.net/php/4181.html">【PHP】「error_log」の出力先を指定してデータを記録 - ysklog</a></li> </ul> <h4 id="関数リファレンス"><a href="#%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9">関数リファレンス</a></h4> <ul> <li><a target="_blank" rel="nofollow noopener" href="https://www.php.net/manual/ja/function.set-exception-handler.php">PHP: set_exception_handler - Manual</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://www.php.net/manual/ja/function.set-error-handler.php">PHP: set_error_handler - Manual</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://www.php.net/manual/ja/function.register-shutdown-function.php">PHP: register_shutdown_function - Manual</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://www.php.net/manual/ja/exception.tostring.php">PHP: Exception::__toString - Manual</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://www.php.net/manual/ja/exception.getmessage.php">PHP: Exception::getMessage - Manual</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://www.php.net/manual/ja/class.errorexception.php">PHP: ErrorException - Manual</a></li> </ul> <h3 id="エラーの種類とフラグ"><a href="#%E3%82%A8%E3%83%A9%E3%83%BC%E3%81%AE%E7%A8%AE%E9%A1%9E%E3%81%A8%E3%83%95%E3%83%A9%E3%82%B0">エラーの種類とフラグ</a></h3> <ul> <li><a target="_blank" rel="nofollow noopener" href="https://qiita.com/kyo_mizu/items/a1d185c7a32019b0f98c">PHPのエラーの種類 - Qiita</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://www.php.net/manual/ja/errorfunc.constants.php">PHP: 定義済み定数 - Manual</a></li> </ul> arm-band tag:crieit.net,2005:PublicArticle/16713 2021-03-03T22:12:17+09:00 2021-03-03T22:12:17+09:00 https://crieit.net/posts/perl-enable-by-htaccess-20210303 .htaccess で Perl を有効にする <p><a target="_blank" rel="nofollow noopener" href="https://labor.ewigleere.net/2020/02/03/perl-operation/">Apache の設定を編集</a>した方が早そうですが、 <code>.htaccess</code> で Perl を有効にしたい場合のメモです。</p> <pre><code class="htaccess">Options +ExecCGI AddHandler cgi-script cgi AddType application/x-httpd-cgi .cgi .pl </code></pre> <p>Apache の conf と代わり映えしないですが、一応メモしておきます。</p> <h2 id="参考"><a href="#%E5%8F%82%E8%80%83">参考</a></h2> <ul> <li><a target="_blank" rel="nofollow noopener" href="https://qiita.com/shotets/items/cf0783530a1f2809c4d5">.htaccess の書き方(設定変更編) - Qiita</a></li> </ul> arm-band