Canonicalization in Rails - routing or .htaccess?

I have a website that is about to begin and a request will be made for the Canonicalization URL. I want to know what is the best way to get all http://www.example.com requests to constantly redirect (301) to http://example.com in my RoR app? Or, I asked another way, how can I remove "www". from all generated urls, paths, requests?

FYI, this is a Rails 3 application.

+4
source share
2 answers

For Apache, you can add the code below to the /public/.htaccess file in your ROR application. I use this for most of my applications because I don't like www

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

Hope this helps

+1
source

This is done using rewrite rules on the web server.

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 must add two A records to your DNS zone file, for example

 @ IN A 10.0.0.1 www IN A 10.0.0.1 

with replacing 10.0.0.1 with your IP address.

+2
source

Source: https://habr.com/ru/post/1339750/


All Articles