5
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?

EDIT:

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]
</IfModule>

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

MrWhite
  • 11,643
  • 4
  • 25
  • 40

4 Answers4

3

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]
Mark Wagner
  • 17,764
  • 2
  • 30
  • 47
  • `^(.+)$` 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 http://www.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"? –  Oct 13 '10 at 23:59
  • Use the leading /. Without the leading / in the RewriteRule was causing / to appear twice. E.g., http://example.com/foo/bar was being rewritten to http://www.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 –  Oct 15 '10 at 00:48
  • And what's about forcing a trailing slash for every subfolder within the domain, e.g. `example.com/foo/bar` will be rewritten to `example.com/foo/bar/` - that would be a kind of "holy grail" IMHO... ;-) –  Oct 15 '10 at 00:58
  • "Use the leading /." - You should only use the leading slash on the `RewriteRule` _pattern_ if these directives are in a _server_ or _virtualhost_ context. In a _directory_ context (or `.htaccess` - as in the question), then the slash prefix should be removed as it simply won't match anything. – MrWhite Aug 24 '18 at 20:33
3

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]

or

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)$ [NC]
RewriteRule ^(.*)$ http://www\.%1/$1 [R=301,L]
cwd
  • 2,693
  • 9
  • 32
  • 47
1
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 (.+)

Prix
  • 4,703
  • 3
  • 23
  • 25
  • thx prix! unfortunately, this rule redirects to "http://www./" –  Oct 13 '10 at 17:05
  • please advise... –  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? –  Oct 13 '10 at 17:35
  • Tried your fix, but still doesn't work. –  Oct 13 '10 at 17:39
  • Hmmm this one is harder then i expected it to be, the reason it does not receive the %1 is because the first rule is meant to catch what does not match with it so it doesnt capture anything on %1 to be used bellow, so you gona need an extra rule to detect what it got. – Prix Oct 13 '10 at 18:51
  • Not sure if this update will work aswell but give it a try and let me know, dont forget to clean you cache so it doesnt reuse pre-cached data to confuse the job. The above one might works for subdomains but might not for domain.com – Prix Oct 13 '10 at 18:52
  • Your "final" answer won't work. It will remap example.com to www.com. – Mark Wagner Oct 13 '10 at 18:59
  • well all i can say is that you need a condition to match in order to have a filled %1 or %N depending on where you set your parathensis, i dont have a server to test these rules right now but i am sure i am close :P – Prix Oct 13 '10 at 19:12
0

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]
MrWhite
  • 11,643
  • 4
  • 25
  • 40
daemonofchaos
  • 1,201
  • 1
  • 8
  • 10
  • 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!? –  Oct 13 '10 at 18:37