mod_rewrite( .htaccess ) で URLの「www」ありなしを統一する。「http」を「https」に統一する。

● 【http://www.hogehoge.com】→【https://hogehoge.com】とする場合の例

www 無しのURIを www.付きのURIに変換する。「http」を「https」に統一する。

.htaccess に保存してサイトのトップに置きます。

<ifModule mod_rewrite.c>
	RewriteEngine On

	# http でのアクセス
	RewriteCond %{HTTPS} off
	RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

	# https でのアクセス
	RewriteCond %{HTTPS} on
	RewriteCond %{HTTP_HOST} ^www\.hogehoge\.com$
	RewriteRule ^(.*)$ https://hogehoge.com/$1 [R=301,L]
</ifModule>

上の書き方でリダイレクト連続する場合は次のようにも書きます。

	# http でのアクセス
   RewriteCond %{ENV:HTTPS} !^on$
	RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

● http でアクセスされた場合に https へリダイレクトさせる

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

関連エントリー

No.233
03/27 15:14

edit

perlモジュール