Remco
Remco

Reputation: 683

redirect non-www to www with correct URL params

I need to redirect non-www to www, with correct URL params. A lot of url's (non-www) already in the google index. I need a config that the params in the url is redirect correct. An example:

nice2stay.com/houses/1

redirect to:

www.nice2stay.com/nl/holidayhouses/nameofthehouse

Ideas?

I just tried the code below on my dev laptop.

constraints(:host => "localhost") do root :to => redirect("http://www.localhost:3000") match '/*path', :to => redirect {|params| "http://www.localhost/#{params[:path]}"} end

This work when the requested url is the root domain, but not when the request url is localhost:/houses Is the path config not correct?

Upvotes: 0

Views: 395

Answers (1)

Thomas Guillory
Thomas Guillory

Reputation: 5729

Edit : I'm sorry I havn't seen the rails tag... The following is still valid : you can do it with your front server (apache or nginx). I guess you can also do the 301 redirect inside the application controller.


It depends of your server.

If you are on apache, ensure that mod_rewrite is installed (this is the case in most of shared hosting), create a .htaccess file at the root of your hosting and type the following rewrite rules in it:

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
  RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

This will force the www. prefix and does a 301 redirect from the non-www to the www urls, which is the best solution for google.

Upvotes: 1

Related Questions