Welcome to WebmasterWorld Guest from 54.161.228.30

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

redirect www to non www

   
9:00 am on Feb 23, 2012 (gmt 0)



Hi everyone.

Am have trouble sorting out a redirect.

Basically I have added:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example\.co.uk$
RewriteRule (.*) http://example.co.uk/$1 [R=301,L]


to my .htaccess but the redirect isnt working properly.

It redirects www.example.com/pagename back to example.com when it should be example.com.pagename.

Thanks in advance

Jack
9:50 am on Feb 23, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



RewriteCond %{HTTP_HOST} !^example\.co.uk$
RewriteRule (.*) http://example.co.uk/$1 [R=301,L]

RewriteCond as written: If the requested domain is anything other than exactly "example.co.uk" (whoops! forgot to escape a \.there) ... then

RewriteRule as written: Redirect to http://example.co.uk keeping the rest of the URL unchanged.

It redirects www.example.com/pagename back to example.com when it should be example.com.pagename

Do you mean that no matter what file you request, you end up on the front page instead of on the page you asked for? There's got to be another rule making this happen. Your rule has the capture () and reuse $1 in the right places, so you have to look elsewhere.

Please say that ".pagename" is a typo for "/pagename" and ".com" is a typo for ".co.uk". Otherwise none of it makes sense.

The rule you quoted should come at the very end of all your redirects, after the more specific redirects. (Does not apply if, ahem, you don't have any other redirects.) Putting it earlier probably won't cause errors, but it may lead to a single request being redirected twice. And one of those later redirects is the one causing the problem.
10:19 am on Feb 23, 2012 (gmt 0)

5+ Year Member



If it's just www to non-www (without the complexities of pagename), most domain registrars allow you to set up A records for example.com, www.example.com and *.example.com. Or you can do a URL redirect at the registrar level as well.

You can also use ServerAlias to do that:
ServerName example.com
ServerAlias *.example.com
5:35 pm on Feb 23, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



An alias doesn't cut it. Requesting one should 301 redirect to the other. This is to avoid both versions returning "200 OK" status as that's a Duplicate Content scenario.

Use the Live HTTP Headers extension for Firefox to investigate the server responses. You're perhaps looking for a double redirect.