Has anyone here had any experience of redirecting through .htaccess for this please? I’m trying to do it, so that whatever anyone puts before the actual domain name, it will always go to the www. version. I’ve been googling and experimenting quite a bit, and the nearest I’ve got it is the code below, but that’s not working either. Any ideas please?
Thanks TB, that code does look very small for what I’m trying to do - would that really divert all 5 examples to the one? Also, would that be the exact code? No mention of a domain name?
That’s the exact code, but it doesn’t take into account https, as none of my sites is using that yet. I’m about to convert one, though, so I also need to find out how to account for that.
This isn’t my forte, so I’m just guessing here, but would it work as required simply by changing the second line code to https://www.? I have no way to test at the moment.
Most times they work out the same, but if there had been any previous rewrite rules applied, then $1 vs %{REQUEST_URI} would produce different results.
Imagine a request for http://site.com/foo:
RewriteEngine on
RewriteRule ^foo$ bar
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This would rewrite “foo” to “bar”, but then nonetheless redirect to www.site.com/foo, disregarding the previous rewrite. Personally, I prefer the (.*) and $1 approach. If there were no previous rewrites, then it works out the same, but if there were previous rewrites, then we don’t throw them away.
Thank you all for the help, but I may have got a bit lost there, sorry How do I ensure that the first 5 addresses below, redirect permanently to the last please?