Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How to redirect everything from no-www to www except for few files, using .htaccess?

I am using the following .htaccess to redirect no-www to www.

.htaccess

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^[^.]+\.[^.]+$ [nc]
rewriterule ^(.*)$ http://www.%{http_host}/$1 [r=301,nc]

How do I add exception for few files?
for example:
1) example.com/page1.html should not be redirected to www.example.com
2) example.com/xml/page2.xml should not be redirected to www.example.com

Everything other then example.com/page1.html and example.com/xml/page2.xml should be redirected to www.example.com

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

You could chain your RewriteCond directives, e.g.:

RewriteEngine on
RewriteCond %{REQUEST_URI} ! ^/page1\.html$
RewriteCond %{REQUEST_URI} ! ^/xml/page2\.xml$
RewriteCond %{http_host} ^[^.]+\.[^.]+$ [nc]
RewriteRule ^(.*)$ http://www.%{http_host}/$1 [r=301,nc]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.