2

Thanks in advance for any help you can provide on this issue! I manage a Ruby on Rails website that uses Apache and lives on Joyent's Cloud Server. Right now I have a pdf located at http://example.com/docs/mydoc.pdf . I'd like for this url to 301 redirect visitors to http://example.com/mydoc , which will be a webpage with the same content (instead of a pdf.)

I've tried a couple of methods for getting the redirect to take place, but neither are working.

  1. I went into VirtualMin and into the Apache server. Then, I chose Aliases / Redirects. Finally, I went under Permanent URL redirects and entered: FROM http://example.com/docs/mydoc.pdf TO http://example.com/mydoc and applied the changes.
  2. Since that didn't work, I went looking for an .htaccess file in my /public folder. None existed. So, I created an .htaccess file and added it to the /public folder. The only code in the file is

Redirect 301 /docs/mydoc.pdf http://example.com/mydoc

The problem? When I go to http://example.com/docs/mydoc.pdf, the redirect doesn't work.

I'm looking for the simplest way to add this 301 redirect and future 301 redirects to my website. Thanks again for your advice!

KDP
  • 634
  • 1
  • 11
  • 31

1 Answers1

1

Rails 3 introduced an easy way to perform 301 redirects from config/routes.rb

match "/oldpath", :to => redirect "/newpath"

This site has a somewhat cumbersome solution for 301 redirects in Rails 2.

However, if I were you I would put all my redirects in an apache config file. This maintains a helpful degree of modularity: you don't have to mix your routes with your redirects, you don't have to mix artifacts of your old website into your new app.

rpedroso
  • 985
  • 6
  • 10
  • Sorry, my mistake! I forgot to mention that the site is built in Rails 2.3.14 (and an upgrade to 3 isn't possible at this time.) How can I do 301 redirects in that version, and is this the preferable way to do it over using an .htaccess file? I'm concerned about the SEO impact. – KDP Jun 26 '12 at 11:34
  • I've updated the response with a link to Rails 2 instructions, and also with a reason to stick with .htaccess – rpedroso Jun 26 '12 at 15:42
  • Thanks for your help! I updated my original question as well. Unfortunately, the .htaccess solution isn't working for some reason. When I go to http://example.com/docs/mydoc.pdf, the redirect doesn't take place. Any ideas why? – KDP Jun 26 '12 at 19:41
  • I've updated the answer once more. Putting your .htaccess file in your rails directory doesn't matter; rails works quite a bit differently than, say, PHP, and your .htaccess file almost certianly isn't in your root directory. Place your redirects in a config file and include that file into your httpd.conf. Any more specific advice would depend heavily on your implementation. – rpedroso Jun 26 '12 at 20:31
  • Thanks for your response. This is a lot more complicated than I expected to put a single 301 redirect in place. I'm worried that messing around with the configuration could cause other problems. Also, I'm unable to find a httpd.conf file where I think it should be (in the /etc folder). Does this link explain a viable alternative: http://stackoverflow.com/questions/3622706/creating-a-rails-route-to-an-external-url ? – KDP Jun 27 '12 at 20:29
  • That link is a viable solution, but I'm wary of creating a redirects_controller just to deal with changed permalinks. You can find your httpd.conf with the command `httpd -V` as HTTPD_ROOT/SERVER_CONFIG_FILE – rpedroso Jun 27 '12 at 21:11
  • Thanks, @Rpedroso, for all of your responses. I'll explore these three options. Would love to give you points for answers, but still waiting to break the 15 pt reputation barrier! – KDP Jun 29 '12 at 12:06