Forum Moderators: phranque

Message Too Old, No Replies

redirecting domain.com to www.domain.com using only .htaccess

need it to be a 301 but don't have access to main config on server

         

inbound

12:26 am on Jan 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think this is going to be stupidly simple but I've never needed to do this before.

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.

inbound

1:26 am on Jan 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think I'm nearly there but there are a couple of issues:

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.

inbound

2:11 am on Jan 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



More to add to this, the .htm pages when asked for come up as code 200 and do not add the www. to the front PLUS all supporting files are downloaded but report back as 304 codes (not changed)?

Any ideas? Have I messed up the server?

jdMorgan

2:40 am on Jan 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have a spurious NOT operator "!" in your code, an unneccessary line, and a problem with the regex pattern in the rewriterule as well.

Try this:


RewriteEngine On
# Redirect non-www to www subdomain.
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

Jim

inbound

3:10 am on Jan 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jim,

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.

jdMorgan

3:30 am on Jan 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Put the code in your Web root directory, or include the directory name as part of the RewriteRule pattern, as in

RewriteRule ^directory/(.*) ...

If I understood what your problem is, one of those methods should fix it.

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

inbound

4:06 am on Jan 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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.

jdMorgan

4:20 am on Jan 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are simply seeing cached copies from your own browser cache.

Flush your browser cache before testing any changes to code in .htaccess or httpd.conf.

Jim

inbound

4:32 am on Jan 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hurrah,

I think that last bit of advice goes to prove that working on sites at 4.30 in the morning does not help, what an obvious thing to miss.

Thanks for all the help, it really was above and beyond. I just hope I can be as helpful to others in another forum.