LoginSignup
35
38

More than 5 years have passed since last update.

.htaccessで特定のページのみhttpsからhttpにリダイレクトする(あるいはその逆)

Last updated at Posted at 2014-03-14

SSLのかかっているお問い合わせフォームからリンク移動する場合に、相対URLの指定になっていると、SSLではない通常のページもhttpsのURLとなってしまい、外部URL呼び出し部分で警告が表示されたりする。

そこで、特定のURLはSSLありのページにリダイレクトするなどの方法をhtaccessを用いて実現してみます。

/form/をhttpsにリダイレクト

.htaccess
RewriteEngine On

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

NOT条件。lpではじまる場合はリダイレクトしない。ついでに301リダイレクトを明示

.htaccess
RewriteEngine On

RewriteCond %{REQUEST_URI} !^/lp/*
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

OR条件。/profile/または/sp/profile/をhttpにリダイレクト

.htaccess
RewriteEngine On

RewriteCond %{REQUEST_URI} ^/profile/ [OR]
RewriteCond %{REQUEST_URI} ^/sp/profile/
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
35
38
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
35
38