Tell Rails to ignore particular URL and let Apache handle - ruby-on-rails

I have a rails app running on my Apache server via Passenger.
Occassionally I am using some PHP scripts for the website, and have placed them in the public directory.
When I go to /php/ I want Apache to handle the request with the PHP parser and have rails ignore it.
Currently I can go to /php/index.php and it works fine. However I need /php/ to work as well but rails keeps looking for the controller to handle it.
I have a feeling this is something to do with apache rewrite rules, but I cant figure it out.
I have used
RewriteEngine On
RewriteRule ^php - [L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
in the /php/.htaccess file but this doesnt work. I still get the page not found error by Rails.

I think you can do this in your Passenger app's main Apache config file - something along the lines of this
<Directory "/.../myapp/php">
PassengerEnabled off
AllowOverride all
</Directory>
inside your VirtualHost block should do the trick (of turning Rails off, at least - turning PHP on is up to you!).
My answer is based off the ModRails Apache documentation - see section 5.6 for more on the PassengerEnabled command.

Related

Rails - .htaccess not forcing users to use https

I want user to only be able to access the https version of the site. I figured out that a way to do this is with a .htaccess file. I've have added the SSL cert and thats all working fine.
I made the .htaccess file and added the following code in it (replacing 'example' with the domain name)
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
I deployed it and visited http://example.com/ and it loaded just fine as http.
I dont know if because its a rails app that I might need to put the .htaccess file in the public folder, or if there is more I need to do. But from what I read this should work.
I'm using google cloud platform to host my site, and I cant find the VM server type so maybe its not the right server type to run .htaccess files on?
I'm hoping someone here knows how to get this working.
With .htaccess try the following:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
However, with Rails you can use the following in your environment config:
config.force_ssl = true

How can I force a URL to have "www." no matter what?

I'm using Joomla 1.5 and Virtuemart 1. It might seem like a weird question but I need my URL to have "www." written in the URL at all times. If the URL doesn't have "www." then the cart functions don't work. If it does have it, then it does work.
Is there a way I can force this? I am using Parallels Plesk 10.3.1 if I can do it on there.
if your server is running on apache, you can add a .htaccess file to your root webdirectory with the following content:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

http to https redirect on openshift rails app

I want my rails 4.0 app on Openshift Online to serve content only over https.
There is a guide which tells to use a .htaccess in the web-root:
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
I followed the guide and put a .htaccess file with the directive in my app-root/repo directory on the openshift cartridge, but nothing happens. The guide talks about a web-root dir. What is the web-root directory of a rails app, or what is a web-root directory on openshift? And is there another way to establish http to https redirect for rails on openshift?
I was in a similar situation as you (but wanting to redirect depending on the Accept-Language http header), and could not find the place where to put the .htaccess on an OpenShift ruby cartridge.
Tentative with .htaccess
I tried to put the .htaccess file in the app-root/repo, app-root/runtime, app-root/data, the ~/ruby/ directories with no success..
So I ended up doing the redirections from the rails app
A solution for your case through Rails
If you want to enforce SSL for all your application you can simply set config.force_ssl = true in your config/application.rb file. Another common use case is to enforce SSL only for your production environment, thus instead of putting this configuration in your general application.rb file, you can set it in your config/environments/production.rb.
In case you need to enforce SSL only for specific controllers, you could also just call the force_ssl method in the top of your controller as:
class MyController < ApplicationController
force_ssl
[...your actions..]
end
My problem & solution through Rails
I wanted to add this .htaccess
RewriteEngine On
RewriteCond %{HTTP:Accept-Language} ^pt [NC]
RewriteRule ^/$ /pt/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^/$ /fr/ [L,R=301]
RewriteRule ^/$ /en/ [L,R=301]
And finally had to do it in the config/routes.rb config file of rails with
get '/', to: redirect { |path_params, req|
"/#{req.env['HTTP_ACCEPT_LANGUAGE'].scan(/^(?:pt|fr)/).first || 'en'}" }
Any help from the OpenShift team to explain where to put a .htaccess for a Ruby cartridge is still very welcome!
There is one solution for Ruby 1.9 for OpenShift 2 with steps for terminal:
cd your_ruby_git_project_folder/public/
vi .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
</IfModule>
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
cd ..
git add . -A
git commit -m 'message'
git push

Remove "www" from all links to my website - nginx

I created a Rails app and I would like when I put any link pointing to my website, the "www" to be removed.
For example when someone enters http://www.mydomain.com/users into the browser address, this to be changed into http://mydomain.com/users when the page loads up.
So if I was working with apache I could add this lines to .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
However I am working with nginx and unicorn (I am not sure if unicorn has something to do with this).
Do I place something like that in my nginx configuration?
Any resource / point to the right direction will be really helpful.
As #Ricoxor answered, you could use a rewrite to get the correct functionality; however, this is listed as an examplary pitfall in the Nginx Pitfalls documentation.
The correct way to do this would be to write multiple server directives as shown by the documentation:
server {
server_name www.domain.com;
return 301 $scheme://domain.com$request_uri;
}
server {
server_name domain.com;
[...]
}
The reason this is more 'correct' is described at the page linked:
There are actually three problems here. The first being if directives.
That's what we care about now. Why is this bad? Did you read If is
Evil? With if directives Nginx is forced to evaluate every request for
all domains. Evaluating every request against if directives is
extremely inefficient. Avoid using if directives and use two server
directives as shown.
Try that :
Apache :
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.yourDomain\.com$ [NC]
RewriteRule ^(.*)$ http://yourDomain.fr/$1 [QSA,R=301,L]
Nginx :
# nginx configuration
location / {
if ($http_host ~* "^www\.yourDomain\.com$"){
rewrite ^(.*)$ http://yourDomain.fr/$1 redirect;
}
}

Apache rewriteCond Rails multi domain setup cache

He People.
I have a rails app running for multiple sites and it has a cache that looks like this:
tmp/cache/adomain.com/the cached files
No this is not picked up by Apache (obviously) and i am trying to set it up
in my httpd.conf. But I wasn't able to get it working.
This is something i tried:
< VirtualHost *:80 >
PassengerMaxPoolSize 20
PassengerPoolIdleTime 0
DocumentRoot /mnt/app/current/public
RewriteEngine On
RewriteCond /mnt/app/current/tmp/cache%{HTTP_HOST}%{REQUEST_URI} -f
< /VirtualHost>
But it doesn't seem to work! (of course I restarted apache with: apache2ctl restart)
I googled a lot! but nowhere i did find a solution.
Looks to me like you're missing a RewriteRule declaration following your RewriteCond.
RewriteCond provides conditional matching of requests, but doesn't take action without a rule to do something.
Probably change to the following, your paths may vary:
RewriteCond /mnt/app/current/tmp/cache%{HTTP_HOST}%{REQUEST_URI} -f
RewriteRule ^/[^.]+$ /YOUR_CACHE_URI/%{REQUEST_URI} [QSA,L]

Resources