Canonicalization in Rails - routing or .htaccess? - ruby-on-rails

I have a site that is about to launch and a request for URL Canonicalization has been made. I want to know what is the best way to have all requests for http://www.example.com to permanently redirect (301) to http://example.com within my RoR app? Or, asked another way, how can I strip the "www." from all generated urls, paths, requests?
FYI, this is a Rails 3 app.

This is done using rewrite rules in the webserver.
For nginx: http://techtitbits.com/2010/07/wwwno-www-rewrite-rules-for-nginx/
For Apache: http://www.boutell.com/newfaq/creating/withoutwww.html
Also note that you should add two A records into your DNS zone file, like so
# IN A 10.0.0.1
www IN A 10.0.0.1
with 10.0.0.1 replaced with your IP address.

For Apache, You can add the code below to your /public/.htaccess file in your ROR app.
I use this for most of my apps, because I don't like the 'www'
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,L]
Hope this helps

Related

how to setup cakephp that runs from url path

So I'm building microservice architecture and I have separate cakePHP installations for Administration and Partners. So https://example.com is going to be served for normal users and that works fine as it's a standard installation.
But I can't figure out how to set up other two that they work as https://example.com/admin and https://example.com/partners. I use nginx to direct traffic to the correct docker container running admin and partners cakephp. Problem is how to let CakePHP know that his root website path is /admin and not just \? Because all the routing gets messed up.
I'm using CakePHP 4.x and PHP 7.2.x
I think you can simply change the App.baseUrl in the cakephp configuration. For more info check the official documentation
I think it's not a good option to create multiple projects for different AUTH (Partner/Administration). Cakephp allow multiple authentication for different roles.
Example: for a school project I had created Admin/School/Teacher/Students using different AUTH and AUTH storage , which never conflict with each other.
In your case there is a solution .
Assuming that you want to use https://example.com/ and Partner and https://example.com/admin as administrator.
1.First host your Partner project in ROOT.
Create a folder , named "admin" in Partner root projects folder.
Copy/Move your admin project code to your "admin" folder.
Change/update your ".htaccess" files in Partner root folder(FOR APACHE).
APACHE CODE
# Uncomment the following to prevent the httpoxy vulnerability
# See: https://httpoxy.org/
#<IfModule mod_headers.c>
# RequestHeader unset Proxy
#</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule admin admin/ [L]
RewriteRule ^(\.well-known/.*)$ $1 [L]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
After "RewriteEngine on " a new line added to bypass the PARTNER cakephp.
NGINX
# nginx configuration by winginx.com
location / {
rewrite admin /admin/ break;
rewrite ^/(\.well-known/.*)$ /$1 break;
rewrite ^/$ /webroot/ break;
rewrite ^(.*)$ /webroot/$1 break;
}

Remove “www” from website link

So, I have a rails project deployed using apache server. Unfortunately, the domain name is not registered with www. So if I do https://mydoin.com it works but https://www.mydoin.com doesn't. Now what I need is that, if someone uses this URL with www https://www.mydoin.com then I want to remove www from the URL. How can I do this? I am using Ubuntu 16.04 and apache 2.4.
First, you need to register the www.mydoin.com DNS, without this, your server will not be reached by your user. After this, you can configure a redirect in your server or rewrite your requisition, for this, follow this link: How to always remove WWW from a url with mod_rewrite?
This code will auto strip the www. for you even if its a subdomain.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
If you want to fix you DNS issue just add a 'A' record with www.yourwebsite.com

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

what's the best way to force a domain with a rails app? ex: example.com instead of www.example.com

My rails app seems to break when it answers on www.example.com, it previously was working fine with just example.com...however I've moved servers recently and would like to know the best way to redirect all www.example.com requests to go to http://example.com/.../
thanks.
This should do the trick, assuming that you have mod_rewrite enabled
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example\.com
RewriteRule ^(.*)$ http://example.com$1 [R=301,L]
It depends on your server setup and there are different ways. You can just cname www in DNS to the root domain, for one way. If you are running Rails behind Apache then you can do it in Apache with mod_rewrite. If you are taking network requests straight into Rails with eg, mongrel (or webbrick) then you may have to configure those servers, or might have to use Rails routes ?
Hope that helps,
adricnet
According to the Apache's name-based virtual host documentation the first virtual host is the default host. I use this to make the first entry a catch-all that redirects every "undefined" request to the main site:
# Default catch-all
<VirtualHost *:80>
# Note the lack of a ServerName
RewriteRule ^(.*)$ http://www.example.com$1 [redirect=permanent]
</VirtualHost>
# Site 1 - www.example.com
<VirtualHost *:80>
ServerName www.example.com
[ the rest of the site config ]
</VirtualHost>
For extra credit you can set up a wildcard DNS entry so that every undefined host (e.g. asdfasdfasdfas.example.com) gets redirected to www.example.com.
You might not want to hear it but I suggest fixing what breaks instead. You've hard coded your domain somewhere in your app, probably in your routing but it is not possible to tell without the specifics of the error(s), and you need to remove that so you, or some other maintainer, won't have to deal with it again in the future.

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