How to redirect everything from www to nonwww except for few files?

  • Thread starter Thread starter account8226
  • Start date Start date
A

account8226

Guest
Hey, I wanted to use the redirection where all my link www.site.com are redirected to site.com !

BUT, the problem is that I want to keep the www.site.com AND the www.site.com/sitemap.xml, I don't want these two links to be redirected because there will be problems.

Thanks you very much for ur awnseres, sorry for my English I'm French !
 
Code:
RewriteEngine on
RewriteRule ^(sitemap.xml)($|/) - [L]
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
 
Thanks for the awnser. But it's still redirecting to domain.com :(
 
Any idea ? Sorry to annoy you :(

How many subfolders and files are in your root?

With the code

Code:
RewriteEngine on
RewriteRule ^(index.php)($|/) - [L]
RewriteRule ^(sitemap.xml)($|/) - [L]
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

All URLs apart from www.domain.com/index.php and www.domain.com/sitemap.xml will be redirected to their non-www counterparts. The problem is there is no real way to put a rule in to stop just www.domain.com/ (with no index.php) from staying there.

If you don't have too many folders and files, you could simply write rules for all of them manually.
 
Thanks for the awnsers. It worked :) For the main domain I think I found a solution to generate my sitemap with domain.com :) Tx again!
 
Top Bottom