Redirection with www.example.com on example.com (apache2, ubuntu 10.04)?

All kind time of day!

Faced with this problem, you must redirect to the website with www.example.com on example.com

Google gave two main Council: it is with mod_rewrite conditions to redirect type
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]


Or direct redirect using virtual hosts type
<VirtualHost *>
 ServerName www.example.com
 Redirect 301 / http://example.com/
</VirtualHost>


The second method doesn't work for me (Firefox gives an error about infinite redirects), the first works, but here's the thing: if writing to the primary address additional parameters, e.g. www.example.com/test_page/then no redirect occurs.

Please tell me how to fix it?

P. S. the sites are not engaged, to understand the syntax of this mod no desire... Only in the most extreme case.

Thank you!
October 8th 19 at 02:29
7 answers
October 8th 19 at 02:31
Solution
Difference in single $ character in the first line
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
October 8th 19 at 02:33
in .htaccess

www
RewriteCond %{HTTP_HOST} ^www.domain\.com$ [NC]<br> RewriteRule ^(.*)$ <a href="http://domain.com/">domain.com/</a>$1 [R=301,L]

www
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]<br> RewriteRule ^(.*)$ <a href="http://www.domain.com/">www.domain.com/</a>$1 [R=301,L]

if suddenly does not plow add on the selected option
Options +FollowSymLinks<br> RewriteEngine On
October 8th 19 at 02:35
If Apache in front of nginx to redirect them organize better
October 8th 19 at 02:37
RewriteEngine on
RewriteCond %{HTTP_HOST} !^se\.solovyev\.org$
RewriteRule ^ se.solovyev.org%{REQUEST_URI} [R=301,L]

Code on github
Sorry, the parser ate:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^se\.solovyev\.org$
RewriteRule ^ <a href="http://se.solovyev.org%{REQUEST_URI}">http://se.solovyev.org%{REQUEST_URI}</a> [R=301,L] 
- Cordell75 commented on October 8th 19 at 02:40
October 8th 19 at 02:39

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ %1%{REQUEST_URI} [R=301,L]


here is a literate version of boilerplate. Virtually the same as comment n. 1
October 8th 19 at 02:41
Thank you all! It seems to work
October 8th 19 at 02:43
One option which I always use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]

Find more questions by tags Apache