css: set PRE’s max-width so it doesn’t stretch the viewport
[mina86.com.git] / posts / ssl-and-dropping-www-with-mod-rewrite.en.html
blob9151797964e880f21fc389c5b24f9893a4147606
1 <!-- subject: {SSL} and dropping <code>www.</code> prefix using <code>mod_rewrite</code> -->
2 <!-- date: 2013-02-10 21:51:43 -->
3 <!-- tags: apache, ssl, mod_rewrite, www -->
4 <!-- categories: Articles, Techblog -->
6 <p>Surprisingly I couldn’t find any HTTPS-aware examples how to drop
7 the <code>www.</code> prefix from web hosts in Apache, so I had to come up with
8 one myself. Firstly, the following lines need to find their way to the end of
9 Apache configuration file (<code>/etc/httpd/conf/httpd.conf</code> or
10 something):
12 <pre>
13 RewriteEngine on
14 RewriteCond %{HTTPS} off
15 RewriteCond %{HTTP_HOST} ^www\.(.*)$
16 RewriteRule ^(.*)$ http://%1$1 [L,R=301]</pre>
18 <p>Secondly, analogous lines need to be added <em>inside</em> of the
19 <code>&lt;VirtualHost _default_:443&gt;</code> directive of mod_ssl
20 configuration file (<code>/etc/httpd/conf.d/ssl.conf</code> or similar),
21 like so:
23 <pre>
24 &lt;VirtualHost _default_:443&gt;
25 <i># … various directives …</i>
27 <i># Here’s what needs to be added:</i>
28 RewriteEngine on
29 RewriteCond %{HTTP_HOST} ^www\.(.*)$
30 RewriteRule ^(.*)$ https://%1$1 [L,R=301]
31 &lt;/VirtualHost&gt;</pre>
33 <p>Now, after a restart, Apache will drop the <code>www.</code> prefix for
34 both secure and insecure connections.
36 <!-- FULL -->
38 <p>To <em>force</em> secure connection and drop the prefix at the same
39 time the part in main configuration file has to become:
41 <pre>
42 RewriteEngine on
43 RewriteCond %{HTTPS} off
44 RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
45 RewriteRule ^(.*)$ https://%2$1 [L,R=301]</pre>
47 <p>To force secure connection only for one domain, say
48 <code>example.com</code>:
50 <pre>
51 RewriteEngine on
53 RewriteCond %{HTTPS} off
54 RewriteCond %{HTTP_HOST} ^(www\.)?(example\.com)$
55 RewriteRule ^(.*)$ https://%2$1 [L,R=301]
57 RewriteCond %{HTTPS} off
58 RewriteCond %{HTTP_HOST} ^www\.(.*)$
59 RewriteRule ^(.*)$ http://%1$1 [L,R=301]</pre>
62 <p>mod_rewrite has also a <code>REQUEST_SCHEME</code> variable and it’s
63 tempting to use it, but it’s value isn’t exactly is
64 advertised and instead it contains scheme and host name which is not
65 that useful.
67 <p>If you want to <em>add</em> the prefix, <a
68 href="http://no-www.org/">check out this site</a>.
70 <!-- COMMENT -->
71 <!-- date: 2014-05-21 19:46:50 -->
72 <!-- nick: todds -->
74 <p>THANK YOU NICK!!!! THANK YOU THANK YOU THANK YOU!!!! I don’t know why this was so hard to find. I doubt you and I are the only ones on the planet that want to do this or that we are the only two clueless people on the planet. Thanks again!
76 <!-- COMMENT -->
77 <!-- date: 2015-04-15 14:13:59 -->
78 <!-- nick: recurse -->
80 <p>You could do things easier and drop the www. in your DNS configuration, remove the www. from ServerName in your vhost config, then forward all requests to www. back to http://domain.com/requesting-page.blah using:
82 <p>RewriteEngine On<br />
83 RewriteCond %{HTTP_HOST} ^www.<br />
84 RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
86 <!-- COMMENT -->
87 <!-- date: 2015-04-23 15:13:16 -->
88 <!-- nick: mina86 -->
89 <!-- nick_url: http://mina86.com -->
91 <p>recurse, what you’re describing is plain HTTP only.