
こんにちは。私の Web サイトが https 経由でのみ、また www サブドメインを通じてのみアクセスされるようにしたいのですが、現在、次の Apache 仮想ホスト構成を使用しています。
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName example.com
ServerAlias www.example.com
# Redirect http requests to https
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://www.example.com%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule .* https://www.example.com%{REQUEST_URI} [R=permanent,L]
#..
</VirtualHost>
<VirtualHost *:443>
ServerAdmin [email protected]
ServerName example.com
ServerAlias www.example.com
# Redirect http requests to https
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://www.example.com%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule .* https://www.example.com%{REQUEST_URI} [R=301,L]
RewriteCond %{SERVER_NAME} !=www.example.com
RewriteRule .* https://www.example.com%{REQUEST_URI} [R=301,NC,L]
<Directory /home/{apacheuser}/www>
SSLRequireSSL
Order allow,deny
Allow from all
LimitRequestBody 512000
</Directory>
#..
</VirtualHost>
残念ながら、正規表現に関する私の知識はかなり限られているので、これがこれを実現する最善の方法かどうか疑問に思っています。
ありがとう
答え1
交換するだけ
# Redirect http requests to https RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule .* https://www.example.com%{REQUEST_URI} [R,L] RewriteCond %{HTTP_HOST} !=www.example.com RewriteRule .* https://www.example.com%{REQUEST_URI} [R=permanent,L]
による
# Redirect http requests to https
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule .* https://www.example.com%{REQUEST_URI} [R,L]
このように、すべてのhttpトラフィックはhttpsにリダイレクトされます。http://example.comリクエスト
次に、https 設定から次の行を削除します。
# Redirect http requests to https RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule .* https://www.example.com%{REQUEST_URI} [R,L] RewriteCond %{HTTP_HOST} !=www.example.com RewriteRule .* https://www.example.com%{REQUEST_URI} [R=301,L]
だからそのまま
RewriteCond %{SERVER_NAME} !=www.example.com
RewriteRule .* https://www.example.com%{REQUEST_URI} [R=301,NC,L]
答え2
%{SERVER_ADDR}
IPを書き換えるのも効果的かもしれない
編集:
申し訳ありませんが、式の右側に変数を使用することはできません。
次のようなことを試してみてください:
RewriteCond %{SERVER_NAME},%{SERVER_ADDR} ^(.*),\1
あるいはダイナミックな方法で
RewriteCond %{SERVER_NAME} !192.168.1.1