Redirecting via the .htaccess

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?

RewriteEngine on
RewriteCond %{HTTP_HOST} ^http://www.example.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^http://example.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^https://www.example.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^https://example.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^example.co.uk$
RewriteRule ^(.*)$ www.example.co.uk/$1 [R=301,L]

I use

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

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?

I’d missed the https in your post when I replied.

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.

I use:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Are there any advantages to using

RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1

over

RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI}

?

None that I know of, and as I believe @dklynn favours {REQUEST_URI}, that’s probably the better one to use - if it makes any difference at all.

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.

4 Likes

Thank you all for the help, but I may have got a bit lost there, sorry :frowning: How do I ensure that the first 5 addresses below, redirect permanently to the last please?

RewriteEngine on
RewriteCond %{HTTP_HOST} ^http://www.example.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^http://example.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^https://www.example.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^https://example.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^example.co.uk$
RewriteRule ^(.*)$ www.example.co.uk/$1 [R=301,L]

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.