Xreaでサイトを運営する上で設定しておきたい.htaccessの設定例。

カスタムエラーページ

あらかじめJekyllが用意している404.htmlを利用する。

# .htaccess
ErrorDocument 404 /404.html

ErrorDocument ディレクティブ (Apache 2.4 ドキュメント)

リダイレクト

全てをリダイレクトする (サイトの移転など)

# .htaccess
RedirectMatch permanent ^/(.*)$ https://otti.xyz/$1

exclude以外の全てをリダイレクトする

# .htaccess
RedirectMatch permanent ^/((?!exclude).*)$ https://otti.xyz/$1

URLの変更 (A.html -> B.html)

# .htaccess
Redirect permanent /A.html https://otti.xyz/B.xml

Redirectに渡せるステータス

未指定時はtempになる

永久に変更permanent301
一時的に変更temp302
他のリソースで置き替えseeother303
永久に削除gone410

その他

ディレクトリ内の一覧を禁止する

# .htaccess
Options -Indexes

特定のドメインからのアクセスを拒否する

# .htaccess
order allow,deny
allow from all
deny from .example.com

特定のIPアドレスからのアクセスを拒否する

# .htaccess
order allow,deny
allow from all
deny from 192.168.1.0/24