Forum Moderators: phranque
How do I set up htaccess to redirect (301) requests for htt*://domain.com to htt*://www.domain.com?
I can do it with directories without a problem so domains pointing to a directory can be redirected to another, but the host of this client has the www and non prefix domain set up as the same thing with no way to redirect or get rid of the non-prefixed one.
The problem is that www has PR and gets ranked but the non-prefixed one is spidered too, don't want it to cause silly errors in search engines.
Thanks for any help.
here is the code for directing domain.com to www.domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST}!^domain\.com [NC]
RewriteCond %{HTTP_HOST}!^$
RewriteRule ^/(.*) htt*://www.domain.com/$1 [L,R]
* in the htt* is to get around no url posting rule (not code)
This works fine to redirect the simple domain.com and it shows www.domain.com in the browser BUT if a filename is asked for such as domain.com/file.htm the it still shows that but does seem to do a redirect (as the PR appears for the page when it wouldn't otherwise), also hovering over links shows that there is still the www missing.
Is it possible to modify the code above to work with any domain rather than a named one? I have about a dozen I want to do this to on the same server, if doing it 12 times is OK for performance then I'd be happy to do it, but if performance would be better and it can be done in one set of code then I would be delighted.
Thanks for that, it probably doesn't work fully due to my file structure on the server.
domain.com >> www.domain.com works (as does htt*://ftp.domain.com to www.domain.com - ftp.domain.com is set to work as website when prefixed by http?)
So far so good, all PR show correctly, as you would expect.
However, as I have all web data one directory off root, the redirected pages such as domain.com/page.htm come back as www.domain.com/directoryname/page.htm which obviously causes an error
I would imagine that there will be a way to remove "diectoryname/" from the $1 bit, I just don't know how, any ideas?
The help is much appreciated as I'm a jack of all trades - master of none, certainly not Apache.
RewriteRule ^directory/(.*) ...
http://ftp.domain.com is an HTTP access to a non-existent subdomain, and so is redirected. It is not an FTP access. .htaccess only applies to transactions using the HTTP protocol.
Jim
It works a treat. all new pages requested without the www are being redirected correctly.
Small Problem:
The pages that were tested with the other version are still giving the error. Will this resolve itself or is a reboot required?
Thanks, you've been a great help, those Apache docs don't make sense unless you know how that string/pattern matching works.