I have a site that is about to launch and a request for URL Canonicalization has been made. I 开发者_StackOverflow中文版want to know what is the best way to have all requests for http://www.example.com to permanently redirect (301) to http://example.com within my RoR app? Or, asked another way, how can I strip the "www." from all generated urls, paths, requests?
FYI, this is a Rails 3 app.
This is done using rewrite rules in the webserver.
For nginx: http://techtitbits.com/2010/07/wwwno-www-rewrite-rules-for-nginx/
For Apache: http://www.boutell.com/newfaq/creating/withoutwww.html
Also note that you should add two A records into your DNS zone file, like so
@ IN A 10.0.0.1
www IN A 10.0.0.1
with 10.0.0.1 replaced with your IP address.
For Apache, You can add the code below to your /public/.htaccess file in your ROR app. I use this for most of my apps, because I don't like the 'www'
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,L]
Hope this helps
精彩评论