XF 1.2 www. Or non www.

oman

Well-known member
I remember I configured something to help force a www on the domain when you type it without. I need to do re directs on my site.

Could you please tell me how I can do both options:

a) Force www. on the domain

b) Force domain without www.

I haven't decided which one I'll do yet, but soon I will so both options would be great.
 
Further, it will be entirely dependent on whether you are using nginx or Apache2 as your web server. Each is configured differently.
This will (in nginx) rewrite the www. to the domain base
Code:
server {
    listen 80;
    server_name www.yourdomain.com domain.com;
    return 301 http://domain.com$uri;
}

Or this should do the opposite and force yourdomain.com to www.yourdomain.com
Code:
server {
    listen 80;
    server_name www.domain.com domain.com;
    return 301 http://www.domain.com$uri;
}
 
Further, it will be entirely dependent on whether you are using nginx or Apache2 as your web server. Each is configured differently.
This will (in nginx) rewrite the www. to the domain base
Code:
server {
    listen 80;
    server_name www.yourdomain.com domain.com;
    return 301 http://domain.com$uri;
}

Or this should do the opposite and force yourdomain.com to www.yourdomain.com
Code:
server {
    listen 80;
    server_name www.domain.com domain.com;
    return 301 http://www.domain.com$uri;
}
Your code doesn't work for me, this is what I'm using:

Code:
server {
  server_name domain.com;
  return 301 $scheme://www.domain.com$request_uri;
}
server {
  server_name www.domain.com;
  [...]
}
 
Your code doesn't work for me, this is what I'm using:

Code:
server {
  server_name domain.com;
  return 301 $scheme://www.domain.com$request_uri;
}
server {
  server_name www.domain.com;
  [...]
}
It may be because I have 5 domains mapped to sayapple.com (which is the example I pulled from). There are several entries above and below the original one I posted. I don't remap to www, so was not sure if that would work or not, therefore why I stated that it should work. If it was a singular domain, then yes, it would be different. The listen 80 is just in case I decide that I want to run a site on a different port for testing (I have a default config file I copy over). I know the first one works on my remapping (www.apple4me.net, www.apple4me.us, www.apple4me.org and www.justsayapple.com as well as the non www prefixed ones). I had assumed that someone that was familiar with nginx would know that they have to have their default server defined as the final one (since nginx precedence is from top down in the config file).
Also, since I don't serve any https, I just hard code the http, as that all the $scheme does is detect either the request is http or https.
I should have pulled from my bogboys.com one, as their are no additional domains listed for it, and just checked at
Code:
server {
    listen 80;
    server_name bogboys.com;
    return 301 http://www.bogboys.com$uri;
}

server {
    listen 80;
    server_name www.bogboys.com;
works, which is what you have reflected - minus the $scheme prefix.
 
Last edited:
Back
Top Bottom