Apache rewriteCond Rails multi domain setup cache - ruby-on-rails

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]

Related

IP Canonicalization , Rails 3 app

I checked my site with online SEO checker and it suggested that I have to fix this error:
Your site's IP xxx.xxx.xx.xxx does not redirect to your site's domain name.
So as suggested I added some code to my htacces file located in public directory. Of course replacing x's with IP adress.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^XXX\.XXX\.XXX\.XXX
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]
But then after app restart I checked again with SEO checker and again it shows the same error.
Here is my full htacces file:
PassengerAppRoot /home3/ecotec11/rails_apps/darbs/
<IfModule mod_passenger.c>
Options -MultiViews
PassengerResolveSymlinksInDocumentRoot on
#Set this to whatever environment you'll be running in
RailsEnv development
RackBaseURI /
SetEnv GEM_HOME /home3/ecotec11/ruby/gems/gems
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.darbs.ecotechno.lv$ [NC]
RewriteRule ^(.*)$ http://www.darbs.ecotechno.lv/$1 [L,R=301]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]
RewriteCond %{HTTP_HOST} ^173\254\28\107
RewriteRule (.*) http://www.darbs.ecotechno.lv/$1 [R=301,L]
Any suggestions?
Thanks
(1) Your actual IP 173.254.28.107 points to a hosting service. The hosting service cannot point to your site because it probably hosts thousands of other sites.
A solution might be to buy a dedicated IP from that hosting service. Depending on their policies, you might (or might not) have access to your own document root, or your public html directory might be your document root.
(2) If this were a dedicated server or at least dedicated IP, then the answer would be that you edited .htaccess located in a wrong place.
Place an .htaccess file
RewriteEngine On
RewriteCond %{HTTP_HOST} ^XXX\.XXX\.XXX\.XXX
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]
in the default document root, the one where the index.html that says "There is no website configured at this address" is located. You can either look for a file named index.html or see what is the default document root in the Apache config file.

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

creating wildcard based subdomain [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Apache rewrite based on subdomain
I am stuck with one problem which not able to solve it. Plz help me to come out of this.
My requirement is:
I want to have each city name as sub domain in my site. Say www.mysite.com, which can have www.delhi.mysite.com, www.bangalore.mysite.com. The list might go endless. My problem is I don't want to create folders for each of the sub domains. I want to handle it in the URL as query string, say www.mysite.com?city=bangalore. This way I can redirect the request to a single file.
I have made the set up LAMP architecture.
In vhost file, it is
<VirtualHost 127.0.0.1:80>
ServerAdmin webmaster#localhost
ServerName www.mysite.com
DocumentRoot /var/www
</VirtualHost>
<VirtualHost 127.0.0.1:80>
ServerAdmin webmaster#localhost
ServerName *.mysite.com
DocumentRoot /var/www
</VirtualHost>
and Also updated .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mysite\.com [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.mysite\.com
RewriteRule ^(.*)$ http://www.mysite.com/index.html [R,L]
But still I am getting 404 error.
Is my approach correct?? Can it be achievable??
Please help me in doing this.
Try something along these lines:
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$
RewriteRule ^/(.*)$ http://www.domain.com/%1/$1 [L,R]
plus a rule to not apply this to the www prefix.
And next time, please use the search function, this has been asked before.
Make sure you have enabled the rewrite module. Try putting the statements into your vhost config instead of .htaccess.

Tell Rails to ignore particular URL and let Apache handle

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.

Remove WWW prefix from your website

How does Stack Overflow (and other web sites) remove the 'www' prefix when it's entered as part of a URL?
Is it a redirect, a rewrite or something else entirely?
Update: I'd specifically like to know in the context of IIS 6
On Apache, it looks like this (inside an .htaccess file):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
An easy way to do this is using the Apache "Redirect" directive:
<VirtualHost *:80>
ServerName www.example.com
Redirect permanent / http://example.com/
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
# remainder of server configuration goes here
</VirtualHost>
The Redirect directive automatically preserves anything following the / in the URL. I find this method easier to read and understand than the Rewrite method.
Firing up Fiddler, we can see that the server responses with a "301 Moved Permanently" status and refers it to http://stackoverflow.com .
Since StackOverflow is hosted on Windows 2k8 IIS7 they set up this redirect straight away in IIS7.
FYI:
a list of HTTP statuses
If you are a .NET developer you might know "Respose.Redirect" , this creates a 302 Object Moved status. Search engines like 301 status codes in this case better, because they know they should not come back to www.stackoverflow.com in the future.
You can do it several ways, using mod_rewrite and redirecting is my favorite. Something like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.cuenca.co$ [NC]
RewriteRule ^(.*)$ http://cuenca.co/$1 [R=301,L]
redirect. the sub-domain "www.stackoverflow.com" would simply redirect to "stackoverflow.com".
You need a default dns entry added pointing to your web server.
ping site.com and verify ip is pointing to webserver, if not you need to get the default DNS entry added.
for a basic setup:
You'll have to add host headers http://www.visualwin.com/host-header/
Create 1 site with a hostheader of www.site.com
In the Home Directory tab, set it to a permanent redirect to http://site.com
Create a 2nd site with a host header of site.com
If you want www.site.com/file.html to redirect to site.com/file.html you will need a more advanced setup with something like ISAPI_Rewrite or use custom 404 pages to do it.
You can do what mod_rewrite does for Apache, with a comparable URL rewriter for IIS. A good one is IIRF. The rule is:
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [I]
RedirectRule ^(.*)$ http://example.com/$1 [R=301]
You can also wildcard the hostname like so:
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [I]
RedirectRule ^(.*)$ http://example.com/$1 [R=301]
IIRF is free to use.
For apache
<VirtualHost *:80>
ServerName yourdomain.tld
ServerAlias www.yourdomain.tld

Resources