Forum Moderators: phranque

Message Too Old, No Replies

My 301 non-www to www not working.....

Another one with .htaccess isues!

         

trillianjedi

10:55 am on Jul 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi guys,

I've never managed to get this to work. I'm re-visiting it again as I have a little spare time.

Have tried it two ways:-


RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.mydomain\.org [NC]
RewriteRule (.*) http://www.mydomain.org/$1 [R=301,L]

and


RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.org
RewriteRule ^(.*) http://www.mydomain.org/$1 [L,R=301]

I have also tried pretty much all of the examples in the other threads on this topic.

Is there a possibility that we have a configuration problem somewhere?

Thanks,

TJ

bobothecat

11:02 am on Jul 11, 2005 (gmt 0)



Here's what I have, works like a champ for me:

RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.domain\.com
RewriteRule ^(.*)$ [domain.com...] [R=301,L]

trillianjedi

11:58 am on Jul 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks bobo, but that one gives me a 500 internal server error.

So I would guess there's something else in htaccess that is conflicting with this (this is the first mod_rewrite that appears in htaccess).

I'll have to see if I can widgetise my entire htaccess and perhaps post it up.

Alternatively, could this error be caused by a misconfigured Apache?

Thanks,

TJ

trillianjedi

12:35 pm on Jul 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



An additional thought - I'm putting this in my existing .htaccess which is in "www".

Should I create a new .htaccess for this in "/" - does that maybe explain the permissions issue?

Anything else that needs to be configured?

TJ

jd01

1:07 pm on Jul 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This and the other code you posted are correct, so there is something else missing/wrong...

RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.mydomain\.org [NC]
RewriteRule (.*) http://www.mydomain.org/$1 [R=301,L]

Have you looked at your logs to see if there is any indication there as to what the problem may be? Is it something like browser cache, cloaking software (no HOST header), or a browser that does not send HOST headers?

For Reference:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mydomain\.org [NC]
RewriteRule (.*) http://www.mydomain.org/$1 [R=301,L]

Will cause an HTTP 1.0 client, which does not send HOST headers to create an infinite loop, so by using:

RewriteCond %{HTTP_HOST} .

Which checks for any single character in the HOST param., the loop is broken.

Hope this helps.

Justin

[edited by: jd01 at 1:08 pm (utc) on July 11, 2005]

Marcia

1:08 pm on Jul 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>RewriteCond %{HTTP_HOST} ^mydomain\.org

The! is missing after _HOST) in that one.

trillianjedi

1:14 pm on Jul 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the explanation Justin, and thanks Marcia.

I think I've tracked down the problem to something else, and nothing to do with the mod_rewrite code, so just looking into that now....

TJ

trillianjedi

1:34 pm on Jul 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmmm... is there anything else that needs to be done to effect this, like a non-www vhost setup in Apache or anything else?

According to the logs, the non-www URL is ending up at Apache's "default" (requests without a known server name).

Thanks,

TJ

jd01

1:40 pm on Jul 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am not very good at the server settings, I just pretend to know a little mod_rewrite, so I will defer to the greater knowledge of Jim or sitz, et al on this one.

I am only posting to ask: why not use the non-www?

More out of curiosity than anything, because it seems people defer to the www. version more often than the non-www and I am wondering why...

Sorry, I can't be of more help.

Justin

encyclo

1:41 pm on Jul 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



According to the logs, the non-www URL is ending up at Apache's "default" (requests without a known server name).

This will need to be fixed in httpd.conf (ie. by the hosting company if it is a shared server). It might be neccessary to add a ServerAlias to the virtual host container:

<VirtualHost *> 
DocumentRoot "/home/user/public_html"
ServerName example.com
ServerAlias www.example.com
</VirtualHost>

trillianjedi

1:42 pm on Jul 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



why not use the non-www?

Probably deserves a thread of it's own that one (I'm sure it's come up many times).

For me it's about cosmetics, nothing more than that.

TJ

trillianjedi

1:50 pm on Jul 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks encyclo (it's our own machine).

OK, even with the alias to the non-WWW version (opposite of what you put in encyclo - maybe that's a problem?) it's not working.

vhost entry:-


<VirtualHost MY IP ADDY :80>
php_value sendmail_from info@example.org
ServerName www.example.org
ServerAlias example.org
ServerAdmin info@example.org
DocumentRoot /home/example/www
ErrorLog /home/example/logs/error.log
CustomLog /home/example/logs/access.log combined
<Directory /home/example/www>
Options FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>

This is what is in the Apache config:-


# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
#DocumentRoot "/var/www/html"
DocumentRoot "/home/<where it is landing currently>/www"

Should the vhost setting with alias override that?

Thanks,

TJ

encyclo

2:08 pm on Jul 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks OK, TJ - I'm wondering if there is a conflict between your virtual host and the default defined virtual host. Another possibility is a DNS misconfiguration. Are you using all IP-based or name-based virtual hosts? Do you have a default-only virtual host?

We need jdMorgan here, I'm getting waaay out of my depth! ;)

trillianjedi

2:31 pm on Jul 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks encylco - I'm rapidly getting out of my depth also ;-)

I'm wondering if there is a conflict between your virtual host and the default defined virtual host.

My thoughts exactly, but I thought (assumed) that the vhost entry/alias would override anything else?

Another possibility is a DNS misconfiguration.

Both the www and non-www point to the same IP.

Are you using all IP-based or name-based virtual hosts?

All IP based.

Do you have a default-only virtual host?

Yes, the first entry in the vhost file is our default and that points to exactly where the non-www URL is now going. So it seems that the subsequent vhost alias is being ignored and the default is being used instead.

TJ