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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.