web-dev-qa-db-ja.com

1ページを除くhtaccess force HTTPS

これは私の現在の.htaccessファイルです:

RewriteEngine On
ErrorDocument 404 /404.html
# 1
RewriteRule ^extra/([0-9]+)/(.*).html?$ pages.php?do=view&id=$1
RewriteRule ^special-bonus/([0-9]+)/(.*).html?$ pages.php?do=bonus&id=$1
RewriteRule ^thankyou/(.*).html?$ static.php?do=thankyou&prod=$1
# 2
RewriteRule ^list.html pages.php?do=list
RewriteRule ^about.html static.php?do=about
# 3
RewriteRule ^404.html static.php?do=404
RewriteRule ^room.html room.php
# 4
RewriteRule ^sitemap.xml sitemap.php

Room.htmlを除くすべてのページでHTTPS(SSL)を強制したいです。どうやってやるの? Apache HTTP Server(ver 2.2.x)を使用しています。

私が使用していたすべてのページでHTTPSを強制するには:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.mycookiedomain.com/$1 [R,L]

それはうまく機能していましたが、今はroom.htmlを除くすべてのページで機能するように変更を加える必要があり、それを行う方法がわかりません。

4
Cassie Kasandr

サンプルを使用すると、コードでoutを使用できます。

RewriteEngine On
# Go to https if not on room.html
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} !^/room.html$ [NC]
RewriteRule ^(.*)$ https://www.mycookiedomain.com/$1 [R,L]

# Go to http if you are on room.html
RewriteCond %{SERVER_PORT} !80 
RewriteCond %{REQUEST_URI} ^/room.html$ [NC]
RewriteRule ^(.*)$ http://www.mycookiedomain.com/$1 [R,L]

おそらく、ニーズに合わせてこれを少し調整する必要がありますが、この例は少なくとも近いものです。

4
closetnoc

これほど簡単にはなりません。エラーファイルにはHTMLファイルの代わりにPHPファイルを使用できるため、行の1つは必要ありません。また、rewritecondとrewriteruleを単純化して修正しました。実際にチェックしている唯一の条件は、接続が標準HTTPポートにあるかどうかを確認することです。接続されている場合は、room.htmlへのリクエストをmycookiedomain.comのroom.phpに置き換えます。これは、HTTPからHTTPに切り替えるためです安全な場合は、デフォルトの302の代わりに実際のリダイレクト(ステータスコード301)を使用する必要があります。

また、最良の結果を得るには、記載されているすべてのファイルと.htaccessが同じフォルダーにあることを確認してください。

ErrorDocument 404 static.php?do=404

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^room.html$ https://www.mycookiedomain.com/room.php [R=301,L]

RewriteRule ^extra/([0-9]+)/(.*).html?$ pages.php?do=view&id=$1
RewriteRule ^special-bonus/([0-9]+)/(.*).html?$ pages.php?do=bonus&id=$1
RewriteRule ^thankyou/(.*).html?$ static.php?do=thankyou&prod=$1

RewriteRule ^list.html pages.php?do=list
RewriteRule ^about.html static.php?do=about

RewriteRule ^sitemap.xml sitemap.php
0
Mike