www to http resolve for Rails app on passenger - ruby-on-rails

I am running a rails app on phusion passenger. I want to resolve all www addresses to http for SEO benefits.
I am using phusion along with the apache server and for performance benefits would like the redirect to be in the httpd.conf file instead of the .htaccess file. I added the redirect in the httpd.conf file but it redirects to the public folder of the application on every url request. I wrote to my hosting provider and got the following response:
Since passenger is configured for your rails application via .htaccess, the direct rules through htaccess will not work. But, htaccess directs will work fine with non rails applications.
If you really wish to redirect www.xyz.com to xyz.com, you have to add a code similar to the following in your application.
-------
before_filter :check_uri
def check_uri
if /^www/.match(request.host)
redirect_to request.protocol + request.host_with_port[4..-1] + request.request_uri
end
end
I think this is not good for performance of the application. Is there any way I can redirect from the httpd.conf file itself?
Here is the httpd.conf file:
This redirects www to the public folder instead of http
<Directory "/home/xyz">
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</Directory>
<VirtualHost 1.1.1.1:80>
SuexecUserGroup "#500" "#501"
ServerName xyz.com
ServerAlias www.xyz.com
ServerAlias webmail.xyz.com
ServerAlias admin.xyz.com
#DocumentRoot /home/xyz/public_html
DocumentRoot /home/xyz/public
DirectoryIndex index.html index.htm index.php index.php4 index.php5
RewriteEngine on
RewriteCond %{HTTP_HOST} =webmail.xyz.com
RewriteRule ^(.*) https://xyz.com:20000/ [R]
RewriteCond %{HTTP_HOST} =admin.xyz.com
RewriteRule ^(.*) https://xyz.com:10000/ [R]
RemoveHandler .php
RemoveHandler .php5
IPCCommTimeout 31
FcgidMaxRequestLen 1073741824
<Files awstats.pl>
AuthName "xyz.com statistics"
AuthType Basic
AuthUserFile /home/xyz/.awstats-htpasswd
require valid-user
</Files>
Alias /dav /home/xyz/public_html
<Location /dav>
DAV on
AuthType Basic
AuthName "xyz.com"
AuthUserFile /home/xyz/etc/dav.digest.passwd
Require valid-user
ForceType text/plain
Satisfy All
RemoveHandler .php
RemoveHandler .php5
RewriteEngine off
</Location>
</VirtualHost>

Related

Apache 2.2.15 with WSS proxy rules issue

Having a problem with 2.2.15 Apache using wws (web socket) rules.
To get 2.2.15 Apache to support web socket, we followed this guide "http://notmyitblog.blogspot.ie/2014/03/websockets-support-for-apache-httpd.html" and have successfully created the module + added it to Apache.
At this point, Apache is throwing a 502 error when we try to access a page using this.
We are not sure where in the overall httpd.conf file this rule should go.
The aim is to configure a chatbot application that runs using ruby-on-rails.
Our application is split between an Application server running ruby-on-rails on puma (10.1.1.2) and this Apache server (10.1.1.1).
Our httpd.conf contains:
<VirtualHost 10.1.1.1:80>
Server Name hostname
ServerAlias www.our.url>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/cable [NC,OR]
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC,OR]
RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
RewriteRule .* wss://10.1.1.2:3000%{REQUEST_URI} [P,QSA,L]
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule .* http://10.1.1.2:3000%{REQUEST_URI} [P,QSA,L]
RequestHeader set X-Forwarded-Proto "https"
<Location /cable>
Require all granted
ProxyPassReverse wss://10.1.1.2:3000/cable
ProxyPassReverseCookieDomain 10.1.1.2 www.our.url
</Location>
</VirtualHost>
Is this format correct? Are any other configuration changes needed for this to take effect?
Is there any sort of test that can be done at server level to verify that the ws is configured and working?

laravel 5.1 only routing to home page, all other routes are coming 404 not found error, after hosting the local working site to the server

I have pushed my laravel 5.1 application to server. In localhost, it works properly. but in server , only the home page is coming, no other routes working. I get 404 not found not found error. jQuery-2.1.4.min.js:4 POST http://mydomainurl/login/validate_login 404 . I feel, it should traverse to http://mydomainurl/applicationName/login/validate_login. I dont know whats happening and where i have to update.
I have enabled mode rewrite in apache and my .htaccess of application's public/ directory is as below
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] RewriteCond %{REQUEST_URI} !^ RewriteRule ^(.*)$ /$1 [L] </IfModule>
I dont know much about .htaccess, but i copied from someone's answer. Please help
After doing all the above modifications,And surfing around net, it didnt work. Finally i understood that it may be because of url problem and just changed the url in ajax, like url: "<?php echo url();?>/login/validate_login", It started working amazingly. Only in local host simply, url: "login/validate_login", works.
Over all, things to be done are:
1: move public folder contents to your server's root folder(name it anything, eg: var/www/html/my_public). Update the .htaccess and index.php of it as explained in other answers. modify server.php, bootstrap/app.php if still didnt work. use url() with the calls. I achieved with this much :-)
The problem is probably caused by your virtualhost config file. It should have "AllowOverride All" directive^
<VirtualHost *:80>
ServerAdmin youremail#yahoo.com
ServerName yoursite.com
ServerAlias www.yoursite.com
DocumentRoot "/var/www/yoursite.com/public"
<Directory /var/www/yoursite.com/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>

Redirecting www to non www on server with wildcard subdomains

I have a Rails+Apache+Passenger setup and my app serves wildcard subdomains. I need all www URLs to redirect to their non www equivalents.
www.example.net should redirect to example.net
www.subdomain.example.net should redirect to subdomain.example.net
My current vhost config is as below
<VirtualHost *:80>
ServerName example.net
ServerAlias *.example.net
DocumentRoot /home/public_html/example.net/current/public
RailsEnv staging
</VirtualHost>
I tried an assortment of rewrite rules in various locations but none took effect. I've checked to make sure that the apache rewrite module is enabled and RewriteEngine is on. Not sure what I'm missing. All help much appreciated!
I solved this issue in my app, as I have logic based on the domain anyway. Place this code in your ApplicationController
class ApplicationController < ActionController::Base
before_filter :check_host
def check_host
if request.host.split('.')[0] == 'www'
redirect_to "http://" + request.host.gsub('www.','')
end
end
end
Could have special cases if some of your hostnames contain "www." for any other reason that you'd have to code for.
You can use moderewrite in your .htaccess file.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.net [NC]
RewriteRule ^(.*)$ http://example.net/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^www\.subdomain\.example\.net [NC]
RewriteRule ^(.*)$ http://subdomain.example.net/$1 [R=301,NC]
This should work in but I not test it.
or this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]

Apache + Mongrel Cluster = Wrong server config!

I'm trying to get Ruby on Rails going on a Windows 2003 Server. I've installed the Mongrel service and Apache (and RoR, etc).
When I serve an app using just Mongrel, everything comes up perfectly.
So, now I am down to the Apache configuration... Apparently I can't seem to get that right. When I visit my pages, I am returned the correct HTML, but it's returned with the Content-Type set to text/plain instead of html or xhtml... In addition, If I try to get to one of the css pages, I get a 500 Internal Server error (served back as HTML, returned with the text/plain Content-Type).
Here is my Virtual Host file (Any help would be VERY VERY VERY appreciated!):
NameVirtualHost *:8080
#Proxy balancer section (create one for each ruby app cluster)
<Proxy balancer://myapp_cluster>
Order allow,deny
Allow from all
BalancerMember http://rails.localdomain.com:3010
#BalancerMember http://myapp:3011
</Proxy>
#Virtual host section (create one for each ruby app you need to publish)
<VirtualHost *:8080>
ServerName rails.localdomain.com
DocumentRoot c:/www/app/public/
<Directory c:/www/app/public/ >
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ProxyRequests Off
ProxyPass / balancer://myapp_cluster
ProxyPassReverse / balancer://myapp_cluster
ProxyPreserveHost On
#SetOutputFilter INFLATE;DEFLATE
#SetOutputFilter proxy-html
#log files
ErrorLog c:/www/log/app_error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog c:/www/log/app_access.log combined
#Rewrite stuff
RewriteEngine On
# Check for maintenance file and redirect all requests
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /system/maintenance.html [L]
# Rewrite index to check for static
RewriteRule ^/$ /index.html [QSA]
# Rewrite to check for Rails cached page
RewriteRule ^([^.]+)$ $1.html [QSA]
# Redirect all non-static requests to cluster
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://myapp_cluster%{REQUEST_URI} [P,QSA,L]
# Deflate
#AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css
#BrowserMatch ^Mozilla/4 gzip-only-text/html
#BrowserMatch ^Mozilla/4\.0[678] no-gzip
#BrowserMatch \\bMSIE !no-gzip !gzip-only-text/html
</VirtualHost>
OK, here's part of the answer. This part deals with the .css and .js files. Apparently it relates to trailing slashes... I had to remove some slashes and add some others...
Removed:
DocumentRoot c:/www/app/public
<Directory c:/www/app/public >
Added:
ProxyPass / balancer://myapp_cluster/
ProxyPassReverse / balancer://myapp_cluster/
Now I can pull up the .css and .js files just fine...
HOWEVER: I am still having the issue of Apache NOT sending the right headers. Right inside the HTML that I'm returning I have this:
But it's STILL returning text/plain (the DefaultType as set in the httpd.conf).
PLEASE, if anyone has any ideas, let me know!!!!!
Thanks
I heavily recommend a linux host for RoR. Unicorn and Passenger are way better tools than mongrel clusters. See github blog post about it.

New Rails + Passenger deployment half working...not sure why?

I'm in the final stages of going round trip through the entire Rails cycle: development -> test -> production (on an external server). I'm very close...but seeing some errors with the production version and don't know enough about Rails' "magic" to troubleshoot it yet...
this works: www.mydomain.com/rails and returns my app's view
but this: www.mydomain.com/rails/ returns (in its entirety):
<html>
<body>
Index from public
</body>
</html>
Where is this coming from!?! I've ruled out Apache. as Apache wouldn't return that kind of html as an index of a directory...
Added: VirtualHost (per request):
<VirtualHost *:80>
ServerName www.mydomain.com
ServerAdmin admin#mydomain.com
DocumentRoot "/Library/WebServer/mydomainweb"
DirectoryIndex "index.html" "index.php"
CustomLog "/var/log/apache2/access_log" "%h %l %u %t \"%r\" %>s %b"
ErrorLog "/var/log/apache2/error_log"
ErrorDocument 404 /error.html
<IfModule mod_ssl.c>
SSLEngine Off
SSLCertificateFile "/etc/certificates/Default.crt"
SSLCertificateKeyFile "/etc/certificates/Default.key"
SSLCipherSuite "ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:+LOW:!SSLv2:+EXP:+eNULL"
</IfModule>
<IfModule mod_dav.c>
DAVLockDB "/var/run/davlocks/.davlock100"
DAVMinTimeout 600
</IfModule>
<IfModule mod_mem_cache.c>
CacheEnable mem /
MCacheSize 4096
</IfModule>
<Directory "/Library/WebServer/mydomainweb">
AllowOverride None
<IfModule mod_dav.c>
DAV On
</IfModule>
Options All -Includes -ExecCGI +MultiViews -Indexes
</Directory>
RailsBaseURI /rails
<Directory /Library/WebServer/rails/myapp/public>
Options -MultiViews -Indexes
</Directory>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^TRACE
RewriteRule .* - [F]
</IfModule>
<IfModule mod_proxy_balancer.c>
<Proxy "balancer://balancer-group">
</Proxy>
</IfModule>
<IfModule mod_alias.c>
Alias "/collaboration" "/usr/share/collaboration"
Alias "/icons/" "/usr/share/httpd/icons/"
Alias "/error/" "/usr/share/httpd/error/"
Redirect temp "/zapp.html" "/index.html"
</IfModule>
Include /etc/apache2/httpd_groups.conf
Include /etc/apache2/httpd_teams_required.conf
LogLevel warn
ServerAlias mydomain.com
ServerAlias images.mydomain.com
</VirtualHost>
My guess is that apache has directory browsing enabled and passenger is routing this request to the public folder which in turn is going to apache direct.
You can try
Disabling directory browsing by disabling Options Indexes (omitting Indexes)
You possibly catch a route in your routes.rb file to that path explicitly and route it to where you wish.
For the second one, does it work if you do www.mydomain.com/rails/mycontroller/show/1?
If it works with "show" in between mycontroller and 1, there's a problem with your routes.

Resources