What I'm trying to do is to redirect my website visitors who enter example.com to www.example.com.
I would do this with a standard redirect, but I don't know how to make a site specific to WWW or non-WWW addresses.
I see that Firefox thinks my site is clearly different at the WWW version, be开发者_C百科cause it reloads it without using the cache. How can I make a non-WWW specific index.html page to redirect them to www.example.com?
Try this mod_rewrite example:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This will append www.
to every request that’s host does not start with www.
and redirect to it with a permanent redirect. The URI protocol is also preserved but you might want to add a condition to only redirect specific request methods (like GET and HEAD).
If you are using IIS simply create a new web site, add "mydomain.com" header and use IIS redirect feature to redirect to www.mydomain.com.
You may want to check this link that discusses it with IIS, and is the way that stackoverflow.com is doing it... you would most likely reverse the way things are working.
In Jeff's example, he is removing the {www.} from the url http://www.stackoverflow.com/, but for you, it sounds like you could use his example and do the opposite.
Jeff Atwood's Example for how they did it for StackOverflow.
精彩评论