Remove "www" from all links to my website - nginx - ruby-on-rails

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;
}
}

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]

Append www in URL for both HTTP and HTTPS

I have some pages on my website that use http and other pages that use https. Now I want to use mod_rewrite to append www to all URLs whether they use http or https.
I have my current code in .htaccess:
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index.php
RewriteCond $1 !^(index\.php|public|assets|robots\.txt)
RewriteRule ^(.*)$ ./index.php/$1 [L]
Please only focus on the first two lines as the rest are for other rewrite rules but I included it in case they were causing any errors for my first 2 lines.
Now this redirects all pages to http, but I want pages using https to be redirected to the right protocol. I tried the solution to a thread on StackOverflow (htaccess redirect for non-www both http and https) but it doesn't work for me.
The answer you linked should work perfectly. If it isn't working, the HTTPS variable probably isn't set. This usually happens because you're not doing the HTTPS on your apache server. Perhaps you have a proxy that does it, or a load balancer? In that case you need to make sure the HTTPS variable gets set. Something like this often does the trick (the example works for amazon elastic load balancers):
SetEnvIf X-Forwarded-Proto https HTTPS=1

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