LoginSignup
2
6

More than 5 years have passed since last update.

[WIP] htaccess 早見表

Last updated at Posted at 2017-06-25

htaccessって本当に便利ですよね。

前提

htaccess を有効にする

httpd.conf
AllowOverride All

Error page

404は404ページしか見せません

.htaccess
ErrorDocument 404 /404.html
ErrorDocument 503 /503.html

Basic Auth

Basic認証。htpasswd を置かないと入れません。

.htaccess
AuthUserFile {path}.htpasswd
AuthGroupFile /dev/null
AuthName "Protected Area"
AuthType Basic
require valid-user

Mod Rewrite

これを知っているとかなり便利

.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^hogehoge.com
RewriteCond %{HTTP} on
RewriteRule ^(.*)$  https://gehogeho.com/$1 [L,R=301]
</IfModule>

Redirect

単純リダイレクト。301

Redirect permanent /~hoge/ http://hogehoge.com

RedirectMatch

単純リダイレクト。301。正規表現OK

.htaccess
RedirectMatch permanent (.*) https://hogehoge.com

cache setting

キャッシュの設定。しないパターン(前者)とするパターン(後者)

.htaccess
<Files ~ ".(gif|jpe?g|png|ico|otf|ttf|eot|woff)$">
Header set Pragma no-cache
Header set Cache-Control no-cache
.htaccess
<Files ~ ".(gif|jpe?g|png|ico|otf|ttf|eot|woff)$">
Header set Cache-Control 'max-age=2592000, public'
</Files>

<Files ~ ".(css|js|html)$">
Header set Cache-Control "max-age=86400, public"
</Files>

IP Block

.htaccess(2.2.x)
order allow,deny
allow from all

deny from 192.168.           # 192.168.*.* を拒否
.htaccess(2.4.x)
<RequireAll>
Require all granted
Require not ip 192.168.    
</RequireAll>

WAF IP許可設定

WAF。wpは特に特定のIP許可したいです。

.htaccess
<IfModule mod_siteguard.c>
SiteGuard_User_ExcludeSig ip
(許可したいIP)
</IfModule>

Charcter Code

最近は、あんまり使わない気がする

.htaccess
php_value default_charset "UTF-8"
php_value mbstring.language "neutral"
php_value mbstring.internal_encoding "UTF-8"

応用

xmlrpc.php無効(wordpress向け)

.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^xmlrpc\.php$ https://%{HTTP_HOST} [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

許可IP以外のIPはbasic認証行き

.htaccess(2.4.x)
<IfModule mod_authz_core.c>
    <RequireAny>
        AuthName "Input ID & Password"
        AuthType Basic
        AuthUserFile {path}.htpasswd
        Require valid-user
        Require ip {IP}
    </RequireAny>
</IfModule>
2
6
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
2
6