Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Httpd Wiki" for change 
notification.

The following page has been changed by pctony:
http://wiki.apache.org/httpd/ScratchPad/RedirectSSL

------------------------------------------------------------------------------
- Describe ScratchPad/RedirectSSL here.
+ = Redirect Request to SSL =
+ Let's say you want http://www.example.com/secure/ to always be sent over SSL 
(I presume here that both the normal and the SSL vhost have the same content). 
You could do this by linking to the correct page from within your HTML pages... 
but there will always be some user who will sneak by it that way.
  
+ [[TableOfContents([3])]]
+ 
+ ----
+ 
+ 
+ === Using mod_rewrite ===
+ {{{
+ <Location /secure>
+    RewriteEngine On
+    ReWriteCond %{HTTPS} !=on
+    RewriteRule .* https://%{HTTP_HOST}:8443%{REQUEST_URI} [QSA,R=301,L]
+ </Location>
+ }}}
+ 
+ '''Note:''' This snippet can also be used inside a directory or vhost 
container. If the SSL port is 443 (the default), you don't need it (or the 
colon) in the RewriteRule, as all browsers which support SSL will automatically 
use port 443; this example redirects to an SSL server on port 8443.
+ 
+ Make sure you have loaded 
[http://httpd.apache.org/docs/trunk/mod/mod_rewrite.html mod_rewrite] and have 
it enabled.
+ 
+ {{{
+    LoadModule rewrite_module modules/mod_rewrite.so
+    RewriteEngine On
+ }}}
+ 
+ === Using virtual hosts ===
+ When using SSL, you will frequently have at least two virtual hosts: one on 
port 80 to serve ordinary requests, and one on port 443 to serve SSL.  If you 
wish to redirect users from the non-secure site to the SSL site, you can use an 
ordinary [http://httpd.apache.org/docs/trunk/mod/mod_alias.html#redirect 
Redirect] directive inside the non-secure VirtualHost:
+ 
+ {{{
+ NameVirtualHost *:80
+ <VirtualHost *:80>
+    ServerName mysite.example.com
+    DocumentRoot /usr/local/apache2/htdocs
+    Redirect permanent /secure https://mysite.example.com/secure
+ </VirtualHost>
+ 
+ <VirtualHost _default_:443>
+    ServerName mysite.example.com
+    DocumentRoot /usr/local/apache2/htdocs
+    SSLEngine On
+ # etc...
+ </VirtualHost>
+ }}}
+ 
+ ----
+ 
+ 
+ 
+ 
+ === SSL Redirect Method (doesn't require mod_rewrite!) ===
+ [http://httpd.apache.org/docs/trunk/mod/mod_ssl.html#ssloptions SSLOptions 
+StrictRequire] forces forbidden access (403) when `SSLRequireSSL` or 
`SSLRequire` decide access should be forbidden. Usually where a 
[http://httpd.apache.org/docs/trunk/mod/mod_access_compat.html#satisfy Satisfy 
Any] directive is used, this denial of access is overridden.  For strict access 
restriction you can use `SSLRequireSSL` and/or `SSLRequire` in combination with 
an `SSLOptions +StrictRequire` Then an additional `Satisfy Any` has no chance 
once [http://httpd.apache.org/docs/trunk/mod/mod_ssl.html mod_ssl] has decided 
to deny access.
+ 
+ [http://httpd.apache.org/docs/trunk/mod/mod_ssl.html#sslrequiressl 
SSLRequireSSL] forbids access unless HTTP over SSL (i.e. HTTPS) is enabled for 
the current connection.[[BR]]
+ [http://httpd.apache.org/docs/trunk/mod/mod_ssl.html#sslrequire SSLRequire] 
forbids access unless HTTP_HOST matches your SSL certificate ''(in this case, 
the certificate is for `example.com` not `www.example.com`)''.
+ 
+ If either of those 2 checks fail (403), then the 
[http://httpd.apache.org/docs/trunk/mod/core.html#errordocument ErrorDocument] 
directive uses a `302` to redirect the browser to `https://example.com`.
+ {{{
+    SSLOptions +StrictRequire
+    SSLRequireSSL
+    SSLRequire %{HTTP_HOST} eq "example.com"
+    ErrorDocument 403 https://example.com
+ }}}
+ '''Note:''' Checking for the correct HTTP_HOST fixes the problem with Basic 
Authentication asking for the username/password twice, and also fixes security 
errors about your SSL certificate.
+ 
+ 
+ === Alternative to above method (doesn't require mod_ssl!) ===
+ {{{
+    RewriteCond %{HTTPS} !=on
+    RewriteRule .* - [F]
+    ErrorDocument 403 https://example.com
+ }}}
+ 
+ 
+ {{{
+    RewriteCond %{HTTPS} !=on
+    RewriteRule .*$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,R=301,L]
+ }}}
+ '''NOTE''': The ''HTTPS'' variable is always present, even if `mod_ssl` 
isn’t loaded! This is useful if a non-SSL server is redirecting to a 
different SSL-enabled server.
+ 
+ 
+ === Redirect everything served on port 80 to SSL ===
+ {{{
+    RewriteCond %{SERVER_PORT} ^80$
+    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [QSA,R=301,L]
+ }}}
+ 
+ 
+ === Redirecting to SSL or non-SSL using relative URIs ===
+ {{{
+    RewriteRule ^/(.*):SSL$   https://%{SERVER_NAME}/$1 [QSA,R=302,L]
+    RewriteRule ^/(.*):NOSSL$ http://%{SERVER_NAME}/$1 [QSA,R=302,L]
+ }}}
+ 

Reply via email to