Strength www. in rails

I usually use .htaccess files to force the domain to use www. (i.e. http://www.example.com instead of http://example.com ):

#Options +Indexes
RewriteEngine on
RewriteBase /

Redirect permanent "www.example.com" "http://www.example.com"
Redirect permanent "example.com" "http://www.example.com"

However, this does not work in the rails application. What are the alternatives to this rail?

+3
source share
3 answers

check this

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(example\.com)(:80)? [NC]
RewriteRule ^(.*) http://www.example.com/$1 [R=301,L]
order deny,allow

Taken from http://www.htaccesseditor.com/en.shtml#a_WWW

Note. The .htaccess file must be placed in the shared folder of the Rails project.

+6
source

If you are using the new version of Rails, an alternative to using Apache mod_rewrite would be to use Canonical Host Rack middleware .

+1

Metal

./script/generate metal www_redirect

app/metal/www_redirect.rb

# Allow the metal piece to run in isolation
require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)

class WwwRedirect
  def self.call(env)
    if env["SERVER_NAME"] !~ /^www\./
      [302, {"Content-Type" => "text/html", "Location" => "http://www.#{env["HTTP_HOST"]}#{env["REQUEST_PATH"]}"}, ["Redirecting..."]]
    else
      [404, {"Content-Type" => "text/html"}, ["Not Found"]]
    end
  end
end
+1
source

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


All Articles