开发者

Canonicalization in Rails - routing or .htaccess?

开发者 https://www.devze.com 2023-02-11 00:26 出处:网络
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.co

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

0

精彩评论

暂无评论...
验证码 换一张
取 消