Website prefix

To ensure that your site always appears with www. at the very beginning, I’ve been working through some coding for it, but the below comes back as an error, can you see where it’s wrong please?

RewriteEngine on
RewriteCond %{HTTP_HOST} ^http://www.site.com$ [OR]
RewriteCond %{HTTP_HOST} ^http://site.com$ [OR]
RewriteCond %{HTTP_HOST} ^site.com$
RewriteRule ^(.*)$ www.site.com/$1 [R=301,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L]

Thank you for the quick response, would that still redirect even if it started with http ?

Yes, the HTTP_HOST doesn’t include the protocol.

You might find this site useful.

It doesn’t work if it’s http://site.com

It should do. I’m sure you’ve checked for typos but are there any other directives in your .htaccess that may cause a problem?

Have checked and no typos and no conflicting directives. Could you try it on yours please.

This is what I use on most of my sites. All it’s missing is the [NC] no case flag.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.eu$
RewriteRule .? http://www.domain.eu%{REQUEST_URI} [R=301,L]

Thank you. Is it okay to use the NC though?

Sure

Thanks Gandalf, this is still the best forum around.

3 Likes

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