2

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

X10
  • 205
  • 2
  • 4

1 Answers1

2

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]
joschi
  • 21,387
  • 3
  • 47
  • 50