0

I'm having trouble redirecting non-www to www in htaccess.

The goal is to have non-www URLs (with or without subfolders) for a particular domain (domain1.co.uk) redirect to their www equivalents.

So:

'domain1.co.uk' should redirect to www.domain1.co.uk
and
'domain1.co.uk/subfolder' should redirect to www.domain1.co.uk/subfolder

I've tried a couple of things, each with undesired results.

Attempt #1:

RewriteCond %{HTTP_HOST} ^domain1.co.uk [NC]
RewriteRule ^(.*)$ www.domain1.co.uk/ [L,R=301]

Result #1: non-www URLs with subfolders redirect to the homepage (so it's impossible to access any subfolders without including 'www' in the URL)

Attempt #2:

RewriteCond %{HTTP_HOST} ^domain1.co.uk [NC]
RewriteRule ^(.*)$ www.domain1.co.uk/$1 [L,R=301]

Result #2:
'domain1.co.uk/subfolder' redirects to www.domain.co.uk/subfolder (as desired)
but 'domain1.co.uk' now redirects to www.domain.co.uk/www/ (strange result)

Any idea what might be happening here?

I'd be happy to post the full content of the .htaccess file if that would help.

Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
mujuw
  • 41
  • 4
  • Possible duplicate of [Redirect non-www to www in .htaccess](https://stackoverflow.com/questions/12050590/redirect-non-www-to-www-in-htaccess) – Fred Gandt Jul 03 '17 at 17:55

1 Answers1

0

To redirect non-www to www, you can use this :

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,NE,R=301]

Clear your browser cache before testing this.

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • Hi starkeen, Thanks for the quick reply. The problem is that I need this rule to work for a particular domain, not just anything that doesn't begin with 'www', since there are different rules for different domains together in the .htaccess file; it's a Wordpress multi-site. Is there a way to achieve that? – mujuw Jul 03 '17 at 18:04