web-dev-qa-db-ja.com

仮想ホスティングでの.htaccess認証を使用したhttpからhttpsへの自動リダイレクト

やあ私は。htaccess SSLによる認証でApacheの仮想ホストを設定しました。 URL https://www.example.com:9004/test.phpを入力したときは問題なく動作しますが、http://www.example.com:9004/test.phpと入力したとき、次のエラーが表示されます。

Bad Request

Your browser sent a request that this server could not understand.
Reason: You're speaking plain HTTP to an SSL-enabled server port.
Instead use the HTTPS scheme to access this URL, please.
Hint: https://www.example.com/  

私のhttpd.confファイルは次のようになります。

 Listen 9004
<VirtualHost *:9004>
    ServerAdmin root@localhost
    DocumentRoot /mnt/work/httpd
    <Directory "/mnt/work/httpd">
    Options FollowSymLinks
     AllowOverride AuthConfig
    </Directory>
  SSLEngine On
  SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP
  SSLCertificateKeyFile /mnt/work/httpd/www.example.com.key
  SSLCertificateFile /mnt/work/httpd/www.example.com.crt
#RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example.com:9006%{REQUEST_URI}
    ServerName www.example.com
    ErrorLog "/mnt/work/log/error_log"
    CustomLog "/mnt/work/log/access_log" combined
</VirtualHost>

そして、私の/ etc/httpd/conf.d/ssl.confファイルは:

LoadModule ssl_module modules/mod_ssl.so

#
# When we also provide SSL we have to listen to the
# the HTTPS port in addition.
#
Listen 9006

Mmy 。htaccessファイルは次のとおりです。

AuthType Digest
AuthName "Protected"
AuthDigestProvider file
AuthGroupFile /dev/null
AuthUserFile /mnt/work/httpd/digest_auth
Require user johan

http:/www.example.com:9004/test.phpを押すと、自動的にhttps://www.example.com:9004/test.phpにリダイレクトされるようにするにはどうすればよいですか。

3
Akki
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_Host}%{REQUEST_URI} [R=301,L]

トリックを行う必要があります。

3

Httpsに9004が必要なようです。 httpの場合9006。 9004ポートのhttp接続を同じ9004ポートのhttps接続にリダイレクトする必要があります。

ただし、設定は http://example.com:9004https://example.com:9006 にリダイレクトします。9006ポートはhttp用であるため、あまり意味がありません。 RewriteRuleで9006を9004に置き換えるだけです。

URLリダイレクトに関する追加情報: http://wiki.Apache.org/httpd/RedirectSSL

0
Thava