RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

...causes a perfect, non-hardcoding 301 redirect from "www to non-www", what would the exact opposite look like?


According to Prix' post I've changed the .htaccess file to the following:

Options +FollowSymLinks
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
  RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

As already mentioned, this redirects to http://www./ unfortunately. Who can help?


Prix almost had it. When you negate the RewriteCond (with !) it doesn't capture so %1 is empty. Two possible solutions:

Dummy RewriteCond:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)$ [NC]
RewriteRule ^/(.*)$ http://www\.%1/$1 [R=301,L]

%{HTTP_HOST} in RewriteRule:

RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteRule ^/(.*)$ http://www\.%{HTTP_HOST}/$1 [R=301,L]
  • ^(.+)$ this will catch subdomain aswell so %1 would do odd things, not a good way to go, same with your second rule. For example, if user typed subdomain.domain.com it would try to make subdomain.domain.com/$1 – Prix Oct 13 '10 at 19:13
  • No, if you typed subdomain.domain.com it would make www.subdomain.domain.com which is exactly what is asked for. I tested these rules on a running apache. – Mark Wagner Oct 13 '10 at 19:34
  • Thank you, guys - you really made my day! Without the leading "/" in RewriteRule both solutions work for me. So, which one is preferred here, maybe less "performance-sapping"? – Lizzy Oct 13 '10 at 23:59
  • Use the leading /. Without the leading / in the RewriteRule was causing / to appear twice. E.g., example.com/foo/bar was being rewritten to example.com//foo/bar – Mark Wagner Oct 14 '10 at 19:46
  • @embobo: With my Apache configuration I cannot confirm this. Using the leading "/" causes a 403 error but it's working just perfectly without it - example.com/foo/bar gets www.example.com/foo/bar . Btw: Would it be possible to exclude subdomains from the rule(s)? www.subdomain.example.com looks a little strange to me... ;-) THX – Lizzy Oct 15 '10 at 0:48

In summation, a clean, tested version of the code:

This works (for me) to redirect www to non-www

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Either one of these work (for me) to redirect non-www to www

RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://www\.%{HTTP_HOST}/$1 [R=301,L]


RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)$ [NC]
RewriteRule ^(.*)$ http://www\.%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.(.+)$ [NC]
RewriteRule ^(.*)$ http://www\.%2/$1 [R=301,L]

the ! means if it does not start with "www..." then send it to www.%1 which is the (.+)

  • thx prix! unfortunately, this rule redirects to "" – Lizzy Oct 13 '10 at 17:05
  • please advise... – Lizzy Oct 13 '10 at 17:17
  • try adding `` before the dot on the url, i update the above code but in general it should catch %1 – Prix Oct 13 '10 at 17:32
  • I've added the whole thing... anything wrong here? – Lizzy Oct 13 '10 at 17:35
  • Tried your fix, but still doesn't work. – Lizzy Oct 13 '10 at 17:39

The following has always worked for me with Apache 2:

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
  • Yepp but I was looking for a "one-fits-it-all", non-hardcoding rule. What I really don't understand is that rewriting from www to non-www works... but not the other way round!? – Lizzy Oct 13 '10 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy