Domain prefixes in .htaccess

Hi, how do you change the .htaccess file, so that if people enter the whole (usual) domain address http//.www.example.com or http.example.com or example.com - in other words, all the usual variations, always go to www.example.com

This should do it.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.sitehere\.com$ [NC]
RewriteRule .? www.sitehere.com%{REQUEST_URI} [R=301,L]

Thanks Ryan, it’s appreciated. Would that also work if it began with http as well?

You can try it; I’m not sure :slight_smile: . I took the htaccess line from my own file and tinkered to make it fit.
No clue if it will work for you though.

Yes it will. The code only looks at the hostname, and doesn’t care if the URL starts with ‘http’ or ‘https’.

Thanks ScallioXTX, you’re a top, helpful bloke! :wink:

Just tried it, and all the other variations go to www… but, when putting in http://websitename.com it returns an error where the website address is repeated twice in the address bar.

That may be an issue with how the hosting is set up. I’d contact your hoster and ask them if they can explain what’s happening. They have more information that we do.

The hosts are saying that there’s nothing on their servers that would interfere with this. Can any of you experts see anything wrong with the below:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} !^http.example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule .? www.example.com%{REQUEST_URI} [R=301,L]

That code doesn’t make sense. Any URL that is different than one of those three will trigger a redirect, but that also means that any of those three will also trigger a redirect (since it’s different than the other two, e.g., www.example.com is not the same as example.com and thus the redirect will be trigggered). Either remove two of the RewriteCond, or drop the OR in both [NC,OR]

As comparison, consider

if ($a != 1 || $a != 2) {
}

which is always true, because that is both to equal to 1 and equal to 2.

I’m not understanding the technicalities of it, sorry - how about the code below:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %HTTP_HOST} !^http://example.com$ [NC,OR]
RewriteCond %HTTP_HOST} !^http://www.example.com$ [NC,OR]
RewriteCond %HTTP_HOST} !^example.com$ [NC]
RewriteRule .? www.example.com%REQUEST_URI} [R=301,L]

All I’m trying to do, is for any other variation, including with a trailing slash, would go to:
www.example.com

In that case it’s

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC,OR]
RewriteRule .? www.example.com%REQUEST_URI} [R=301,L]

Thanks Scallioxtx, that’s worked. (Where’s the thumbsup smilie when you need it?) One thing, why the OR ?

:thumbsup: :thumbsup:

:wink:

Slip of the fingers, you can remove the OR

Many thanks Scallioxtx, the helps been appreciated. :thumbsup: :wink:

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