3

I want to force a www. prefix on my website by using a .htaccess 301 redirect. I am currently trying:

RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]

Which normally works, but I am using Zend Framework which causes all requests to be redirected back to http://www.mysite.com/index.php regardless of the initial request.

For example...

http://mysite.com/blog, 
http://mysite.com/contact,
http://mysite.com/blog/this-is-my-article,

Will all be redirected to http://www.mysite.com/index.php

However, if I initially request a specific file, such as...

http://mysite.com/some-file.htm

The redirect works properly, redirecting to http://www.mysite.com/some-file.htm

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
jeff1234567890
  • 163
  • 4
  • 8

2 Answers2

4

In first time, don't forget to enable the rewriting ("RewriteEngine on"). The last line is important if you use Zend Framework.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
RewriteRule !\.(pdf|php|js|ico|txt|gif|jpg|png|css|rss|zip|tar\.gz)$ index.php

Now the url...

http://mysite.com/some-file.htm

... redirect to http://www.mysite.com/some-file.htm but use the index.php

Kevin Campion
  • 2,223
  • 2
  • 23
  • 29
1

Don't forget to ignore sub-domains when re-directing to www.

http://www.theblogaholic.com/2011/01/16/force-www-using-htaccess-except-for-subdomains/

Jim
  • 11
  • 1