How can I force 'www' subdomain w/ Apache2 + Rails + Phusion Passenger? - ruby-on-rails

My clients want to use 301 redirects to force the 'www' subdomain on their sites. So 'example.com/something' resolves to 'www.example.com/somthing' etc.
What I'm trying to do is simply add this to my vhost file:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName example.com
ServerAlias www.*
DocumentRoot /data/apps/example.com/current/public
RailsBaseURI /
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
</VirtualHost>
I've also ensured that mod rewrite was enabled via:
sudo a2enmod rewrite
sudo /etc/init.d/apache2 force-reload
The outcome of my current efforts basically appears to be successful. Apache restarts and everything works as expected EXCEPT the rewrite isn't happening. So both 'www.example.com' and 'example.com' resolve. The browser doesn't get redirected to 'www.example.com' when it should. Any ideas? I've tried reloading the configuration and restarting apache several times but I can't seem to get the rewrite rules working. Am I setting them up in the wrong place by placing them here in the vhost instead of a .htaccess file?
Any advice here would be useful. I'm completely baffled.

Put this below your main VirtualHost entry:
<VirtualHost *:80>
ServerName example.com
RedirectMatch permanent ^/(.*) http://www.example.com/$1
</VirtualHost>
You main VirtualHost should have a ServerName www.example.com entry and no aliases or redirects. Also, mod_rewrite is not required for this redirect.

Related

Apache rewrite rule only working when rewriting root

I have an apache server running on localhost:8888. The following Virtual Host rule points to my rails application on localhost:3000:
<VirtualHost *:*>
ProxyPreserveHost On
RewriteEngine on
RewriteRule "^/(.*)$" "http://localhost:3000/$1" [P]
ProxyPass "/ia" "http://localhost:3000"
ProxyPassReverse "/ia" "http://localhost:3000"
ServerName localhost:3000
</VirtualHost>
This works fine but the re-write rule is sending all requests to the rails server and I only want it to rewrite requests following http://localhost:8888/ia/*. I have tried:
RewriteRule "^/ia/(.*)$" "http://localhost:3000/$1" [P]
I have also tried:
RewriteRule "^\/ia\/(.*)$" "http://localhost:3000/$1" [P]
but this doesn't seem to re-write anything. Essentially, without the rewrite rule, if I go to http://localhost:8888/ia none of the images/css will render because it's not re-writing links to localhost:3000. What am I missing?

Facebook shared link does not open in FB iOS App in-app browser

We have a website deployed in a Ubuntu 14 server running Apache. The website uses an SSL certificate (so you can access through https) and has a redirection for all the incoming requests without https.
Ok, so the website is working as expected in desktop browsers and mobile browsers, but, when users tap on the links (from the Facebook iOS app) the in-app browser tries to open the website and returns a message like "Link doesn't work, try again" (not the exact message in english).
After a lot of research I still don't have a clear idea of what this can happen (just found a couple of unresolved cases on google), so I'd start by looking at the server configuration.
The apache configuration (IP and domains are samples):
<VirtualHost 200.200.200.200:80>
ServerName www.domain.com
Redirect permanent / https://www.domain.com/
</VirtualHost>
<VirtualHost domain.com:80>
ServerName www.domain.com
Redirect permanent / https://www.domain.com/
</VirtualHost>
<VirtualHost www.domain.com:443>
ServerName www.domain.com
ServerAlias domain.com
SSLEngine on
SSLCertificateFile /home/ssl/www.domain.com.crt
SSLCertificateKeyFile /home/ssl/domain.com.key
ServerAdmin admin#domain.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
The .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
The previous configuration works fine in all browsers, it just doesn't load (doesn't even makes it to Apache access log) when loaded with Facebook iOS in-app browser.
Help is much appreciated! Thanks in advance for your time.
After analyzing the url with the SSL Checker suggested by #CBoroe, I find out that there was a missing intermediate certificate configuration that needed to be added because we're using Apache < 2.4.8 version.
We just had to add the SSLCertificateChainFile /home/cert_ssl/intermediate.crt directive. The final SSL part of the code looks like this:
SSLEngine on
SSLCertificateFile /home/ssl/www.domain.com.crt
SSLCertificateKeyFile /home/ssl/domain.com.key
SSLCertificateChainFile /home/ssl/intermediate.crt
Hope this helps someone else having this issue. After this change, restart Apache with:
service apache2 restart
And everything should work fine (you can use the same SSL checker tool to validate it).
Thanks for your time!

How to remove web/ from url (symfony)

I am using symfony 3. I want to remove web/ from url like this.
localhost/symfony/web/
to
localhost/symfony/
but I want all my asset (css, js etc) in web folder.
Like other frameworks (codeigniter, cakephp), we access main controller from root not web folder
Anybody can help
I am using this htaccess in the root directory. It could help you.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ web/$1 [QSA,L]
</IfModule>
I'd suggest to change the DocumentRoot path in your Virtual Host configuration in Apache.
<VirtualHost *:80>
...
DocumentRoot /path/to/project/web
</VirtualHost>
This way Apache will point to your web/ folder instead of your project root folder and will avoid having the /web/ in your URL.
Hope this helps :)
You can remove it by changing the DocumentRoot path in your Virtual Host configuration in Apache.
Step-1
Open D:\xampp\apache\conf\extra\httpd-vhosts.conf and put below code
<VirtualHost *:80>
ServerName mysite.dev
ServerAlias www.mysite.dev
DocumentRoot "D:/xampp/htdocs/symfony_demo/web"
<Directory "D:/xampp/htdocs/symfony_demo/web">
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
# uncomment the following lines if you install assets as symlinks
# or run into problems when compiling LESS/Sass/CoffeeScript assets
# <Directory /var/www/project>
# Options FollowSymlinks
# </Directory>
ErrorLog "logs/mysite_error.log"
##CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
And save this file
Step-2
Open C:\Windows\System32\drivers\etc\hosts file and put below code
127.0.0.1 mysite.dev
and save
Step-3
Restart Apache and hit "mysite.dev" in browser

Virtual host with Passenger

Rails app already works correctly on mydomain.com with Apache+Passenger. In addition, I'm going to deploy non-Rails app(wordpress) on blog.mydomain.com. So I modified httpd.conf like
PassengerEnabled off
<VirtualHost *:80>
ServerName mydomain.com
DocumentRoot /var/www/railsapp/public
PassengerEnabled on
</VirtualHost>
<VirtualHost *:80>
ServerName blog.mydomain.com
DocumentRoot /var/www/blog
</VirtualHost>
But this doesn't work. blog.mydomain.com also shows Rails app. How can I divide them?
Try adding this to the top of the conf file:
NameVirtualHost *:80
You might also try swapping the order so the more specific one is first, but look at the section called 'Using the ServerPath Directive' on this page. It indicates that with older http/1.0 clients, you may not have enough information to route it correctly.

Passenger: RailsBaseURI case sensitive?

I used Passenger to deploy a RoR app to a sub URI on my domain. The problem I'm facing is that the sub URI seems to be case sensitive. Navigating to http://mydomain.com/RailsApp resolves fine. However, if I go to http://mydomain.com/railsapp, http://mydomain.com/railsApp, or any other variation, I get a 404 error. How can these requests using different casings get resolved correctly?
Here is my Apache configuration file:
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias www.mydomain.com
DocumentRoot /www/mydomain/public
<Directory "/www/mydomain/public">
RailsEnv "production"
Order allow,deny
Allow from all
</Directory>
RailsBaseURI /RailsApp
<Directory "/www/RailsApp/public">
RailsEnv "development"
Options -MultiViews
</Directory>
</VirtualHost>
Any help is much appreciated. Thanks!
You could look into using mod_rewrite and matching it case insensitive there.
Some links to get you started :)
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
http://httpd.apache.org/docs/2.2/misc/rewriteguide.html
Thanks ba for pointing me in the right direction.
I did some research and found the mod_speling module. This not only makes the URL case-insensitive but also checks for spelling errors.
To enable mod_speling:
sudo /usr/sbin/a2enmod speling
sudo /etc/init.d/apache2 force-reload
sudo /etc/init.d/apache2 restart
To use mod_speling, include the directive CheckSpelling on in your virtual host section:
<VirtualHost *:80>
CheckSpelling on
...
</VirtualHost>

Resources