glFusion Wiki

Site Tools


glfusion:redirect

URL Redirection

When you initially install glFusion, you configured a Site URL setting. We want to make sure that if you site is accessible through multiple URLs, that we always redirect to the URL setup in Site URL.

Many shared hosting providers allow access to a site by using both http://example.com (notice the missing www) and http://www.example.com.

I always just type the root domain, example.com. But for sites that I manage I choose www.example.com to be canonical – mainly because in print it’s best to use the www. address as it’s easy to pick out as a web address. For .com’s this isn’t as important as for .org or .net, IMO, as nowadays most people recognise that “.com” refers to a website.

Why This is Important

glFusion utilizes Cross Site Request Forgery (CSRF) protections that validate the referring URL to the configured URL. If they do not match, the request is not executed. If you can access a site through multiple URLs, there is a possibility that requests will fail the CSRF check. For example, when logging into a site, http://example.com/users.php is not equal to http://www.example.com/users.php.

glFusion also uses the full URL when creating links to style sheets (.css) and JavaScript (.js) files. Modern browsers will block access to some of these files or images when the URLs are not matched.

Another reason why it is preferable to only allow access via a single URL is search engines. If there are multiple URLs, you are showing the same content to the search engines under more than one URL. In other words, your site is serving up duplicate content.

The problem is that search engines have to choose *one* of those copies, and declare it to be the official “canonical” URL.

Setting Up Redirection using .htaccess

Add the following to your .htaccess file

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

This will redirect all addresses that don’t start www.example.com to www.example.com.

Setting Up Redirection in Nginx Web Server

If you want redirect users from a plain, non-www domain to a www domain, add this server block:

server {
  server_name example.com;
  return 301 $scheme://www.example.com$request_uri;
}

Save and exit. This configures Nginx to redirect requests to example.com to <nowiki>www.example.com</wowiki>. Note that there should be another server block that defines your www web server.

To put the changes into effect, restart Nginx:

sudo systemctl restart nginx
glfusion/redirect.txt · Last modified: 2017/04/12 21:09 (external edit)

Page Tools