.htaccess各種設定方法


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

今回はhtaccessについてです。サイト運営するときはまずコレ設定しておきましょう!wwwがあるパターンと無いパターンですがどちらかに統一しておきましょう。

アクセスが分散されるのでSEO的にもよろしくないです。

.htaccessファイルに下記のように記述してアップしておきましょう!

.htaccessって何?と言われてなかなか簡単には伝えられないですね。。。。。
なんだろう。。。

Apacheを制御する設定ファイルです。

あぱっちって何?Webサーバソフトウェアです。。。。

まあ、結構いろいろと出来ますよ、、といっておきましょう。

具体的には認証とかIP制限など、あとはリダイレクトなどが有名ですね。


スポンサーリンク

その1.wwwありなしの設定の記述例

WWWあり

1
2
3
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(php-fan\.org)(:80)?
RewriteRule ^(.*) https://php-fan.org/$1 [R=301,L]

WWWなし

1
2
3
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.php-fan\.org)(:80)?
RewriteRule ^(.*) https://php-fan.org/$1 [R=301,L]

因みにこのサイトはないパターンです。

その2.リダイレクト

これはページを飛ばすための設定です。
下記の例はエラーがあった場合はこのページに飛んでねってことを記述しています。

1
2
3
4
ErrorDocument 403 /error/403.html
ErrorDocument 404 /error/404.html
ErrorDocument 500 /error/500.html
ErrorDocument 503 /error/503.html

エラー番号403は部からのアクセスが禁止されてるファイルにアクセする場合、一般にアクセス出来ないページなどに表示されます。
エラー番号404はページが存在しないかファイルがないかです。
エラー番号500はCGIの設定が間違っているもしくは使えない状態にあるかです。
エラー番号503はアクセスが多すぎて一時的に負荷が生じているかメンテナンス中の状態です。

100番台は情報、200番台は成功、300番台はリダイレクト、400番台はクライアントエラー、500番台はサーバーエラーと分類されています。

次の例は特定のアドレスにきたら今のアドレスに飛ばす方法

1
Redirect permanent /php/ https://php-fan.org/php/

http://ドメイン/php/以下はすべてhttps://php-fan.org/php/以下に飛ばすというディレクトリ構造を保ったままリダイレクトされるのが良いですね。
以下の例はすべてのドメインに来たら新アドレスに飛ばす方法です。

1
RewriteRule ^(.*)$ https://php-fan.org/$1 [R=301,L]

その3.特定のホストやIPアドレスからのアクセスを全て禁止する

1
2
3
RewriteCond %{REMOTE_HOST} ^php-fan\.org$ [NC, OR]
RewriteCond %{REMOTE_ADDR} ^192\.168\.1\.
RewriteRule ^.* - [F]

その4.ベーシック認証

特定のページに来たらIDとパスワードを尋ねる方法です。

1
2
3
4
5
6
7
8
9
AuthUserFile /home/sites/web/php-fan.org/.htpasswd
AuthGroupFile /dev/null
AuthName BASIC-AUTH
AuthType Basic
require valid-user
 
<Files ~ "^.(htpasswd|htaccess)$">
    deny from all
</Files>

上記のAuthUserFileのところに.htpasswdファイルの作成しないとダメです。
PHP:fanorg
IDとパスを記述してアップすれば出来上がりです。

その5.特定の拡張子に変更

phpファイルをhtmlにする場合は以下で出来ますが、結構な負荷がかかると思いますので、あまりおすすめしません。
アクセスが少ないサイトなら良いと思いますが、サーバー側でいちいちファイルを変更しているのでやらなくても良いと思います。

1
AddType application/x-httpd-php .htm .html

以上です。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です