sonnyhe2002
sonnyhe2002

Reputation: 2121

Rails Redirect Error on Safari

We are running a Rails 3.2.11 app on Heroku, and we see intermittent problems with Mac/Safari browsers. When a user clicks a link or enters a link manually into Safari without a trailing slash (e.g. http://myapp.com/product/1), the browser returns a 404 status code. If this is happening to one user, a different user on a different Mac can navigate to the identical link without a problem.

If the user then adds a trailing slash (e.g. http://myapp.com/product/1/) then the link will work properly. Not only will it work properly the first time, it will continue to work properly any time they use the link thereafter.

Upvotes: 1

Views: 479

Answers (2)

Hyra
Hyra

Reputation: 828

The solution seems to be to force a trailing slash. Depending on the server setup you got going you could add some rewrite rules.

If you are running apache it would look something like this:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]

For nginx it would be

server {
    listen 80;
    server_name www.mysite.com;
    rewrite ^(.*[^/])$ $1/ permanent;
}

Upvotes: 0

Josh Williams
Josh Williams

Reputation: 68

My first instinct would be to blame Safari (sort of) and say that the original request (without the '/') is resulting in a redirect from the server, which Safari is somehow following incorrectly.

The only way to really track it down would be to watch the server logs (or sniff the client traffic) to see what is really transpiring between client and server.

Upvotes: 1

Related Questions