Forum Moderators: phranque

Message Too Old, No Replies

301 for non-www. to www. not working, plus custom error stops working

         

webspud

3:21 pm on Apr 21, 2005 (gmt 0)

10+ Year Member



I'm trying to set up a 301 redirect to ensure all non-www. requests get redirected to www. addresses. Please excuse me I'm a novice with this and not at all technical, I need simple to understand answers!

The site has an existing (working) .htacess file with code to redirect to a custom 404 supplied by the ISP. I've tried adding the code below (and several variations) but the response code does not change and the custom 404 no longer works.

------------------
RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.my-site\.co\.uk [NC]
RewriteCond %{HTTP_HOST}!^$
RewriteRule ^(.*) [my-site.co.uk...] [L,R=301]

ErrorDocument 404 /~directoryname/404.html [<---this is the existing 404 code which stops working when I add the new code.]
------------------

The results were:
[my-site.co.uk...] = Server Response Code: 200
[my-site.co.uk...] = Server Response Code: 200
[my-site.co.uk...] = Server Response Code: 404, but the custom 404 no longer works.

Is the code ok?
Could it be something to do with the way the server is set up (I'm on shared hosting)?

Any help greatly appreciated!

claus

4:26 pm on Apr 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld webspud :)

It seems like you've done it almost right. Let's take one thing at a time. Rewrites first.



First, i'd change your second condition to become the first. What happens is that the server first checks for the host name, then it checks to see if there is a host name. It's not an error, but it's backwards

So, the first line becomes:

RewriteCond %{HTTP_HOST}  !^$ 

-which you can also write as this:

RewriteCond %{HTTP_HOST} .

The second line checks to see if the host name is okay:

RewriteCond %{HTTP_HOST}  !^www\.my-site\.co\.uk [NC]

And then the rule redirects all queries. You did forget a "$" in what you posted above, but i don't think this is a serious error. However, i include it here, and i also change the order of yor L and R flags, which might matter a bit more (but i'm not totally sure about this):

RewriteRule ^(.*)$ http://www.my-site.co.uk/$1 [R=301,L]

All that yields the following, which is totally standard, and should not give errors in any normal cases:

-------------------------------------------------- 
# if there is a host name, AND
RewriteCond %{HTTP_HOST} .
# the host name is not www.my-site.co.uk
RewriteCond %{HTTP_HOST} !^www\.my-site\.co\.uk [NC]
# rewrite all requests to the right host name
RewriteRule ^(.*)$ [my-site.co.uk...] [R=301,L]
--------------------------------------------------

If you implement the above in your root ".htaccess" file, i am 99.99% certain that your www-redirects will work as intended. Remember that there must be a space before any "!" - this board tends to eat them in posts.



Then, your error document. What you wrote in your post is exactly the right way to do it:

ErrorDocument 404 /~directoryname/404.html

So, as you did it exactly the way you should, why doesn't it work?

One clue could be the tilde character ("~"). Usually on shared hosting this designates a user account folder. Eg. "/~webspud/" would be the home directory for the user "webspud".

Those user folders might appear to be somewhere else in the file system than they physically are. What you see as eg. "/home/www/~webspud/" might in fact be, say, "/var/users/accountXYZ/some-folder/webspud/" internally. Even if they are not, you might be working against some other rules on the server. Anyway, there can be a lot of different setups, so even if i type a long list yours might be different. Plus, i might be wrong.

What you should make sure about is that the folder called "/~directoryname/" is in fact a subfolder of the folder in which your .htaccess file is placed. So, make a setup like this, in which you try to avoid tilde characters:


/www/ <- your www folder
/www/.htaccess <- your root .htaccess
/www/errorpages/ <- folder for your error pages

...and then put this line in your root ".htaccess" file:

ErrorDocument 404 /errorpages/404.html

Hope this helps.

claus

4:39 pm on Apr 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry about all the typo's in the post above. I'm not going to edit them, as then the board will reformat all code and eat all the extra spaces i put in. There are no typo's in the code parts.

webspud

7:48 pm on Apr 21, 2005 (gmt 0)

10+ Year Member



Claus: Wow! I'm amazed and very grateful for the speed and thoroughness of your answer! Thank you.

I changed the 404 error code so the ("~") is no longer required and that works just fine on its own.

But unfortunately adding the 301 code (using it exactly as suggested) results in the same problem (i.e. both versions still give a 200 Server Response Code and the custom 404 page is no longer accessed).

Any more ideas anyone?

claus

8:38 pm on Apr 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Then it was a good thing i only said i was 99.99% sure *lol*

It sounds as if your server is not set to do rewrites. Did you remember to turn the rewrite engine on? I omitted this at the top:

RewriteEngine on 

Otherwise you should ask your host if they have specified:

AllowOverride All

- in the server configuration file. This specifies what things an .htaccess file is allowed to manage. One option is AllowOverride FileInfo - that one allows htaccess to use custom error documents, so that's one you have already as your 404 is working.

webspud

10:36 pm on Apr 21, 2005 (gmt 0)

10+ Year Member



Yes, the rewrite engine was turned on, so it sounds like it might be your second suggestion.

I'll ask my host about it in the morning (I'm on UK time); I just hope I get a more helpful response from them than I did earlier today! I'll report back when I've spoken to them.

Thanks for the help once again.

webspud

2:49 pm on Apr 26, 2005 (gmt 0)

10+ Year Member



Just to update as promised and incase it helps someone else to know ...

It would appear that it probably was a problem with the server configuration file. When I rang them they said it was set to 'AllowOverride All', but it still didn't work. I then emailed in the problem and the next day they emailed to say it was working now (although didn't actually explain what the problem had been!).

Thanks once again to claus for your help.

claus

7:02 pm on Apr 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



great you got it working (or the host did) :)

McMohan

11:20 am on May 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



An interesting thread and made me do away with posting a new thread. Claus, amazing as usual.

Taking the discussion forward, I will give an example of the 301 I implemented to one of the sites. The code in .htaccess file looks like this -


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

The server head checker shows a 301 status for the non-www version. Implemented about 2 months ago, yet to see the updation in all the 3 big engines. Wondering if there is anything wrong in the hindsight. One thing I noticed, which I am not sure has any bearing at all is, the server head checker details -


Status: HTTP/1.1 301 Moved Permanently
Server: Apache/1.3.33 (Unix) mod_fastcgi/2.4.2 FrontPage/5.0.2.2635 mod_jk/1.2.5
Location: ht*p://www.mysite.com/
Keep-Alive: timeout=15, max=99
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-1

The "Connection: Keep-Alive" part. Seen many site with "Connection: close"

(I am not a server guy, so please excuse my blunders if any)

claus

5:58 pm on May 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In msg#382 here [webmasterworld.com] Dayo_UK noted that nowadays it can take "a few crawl cycles" from you make these changes until you see the results. This could mean 2-6 months.

Google has never been quick on these matters, but there are usually differences from page to page. Some pages change faster than others; I'm not sure why or how, but i guess that it might be the better linked ones, or more frequently updated ones that change first.

>> Keep-Alive

This is a setting on your server that specifies that it will not close the connection to the client. So, it's a friendly server you have got there.

Apart from this, i don't really know if this is good or not. I would think that your server could perform better if connections were closed in stead, but otherwise i don't think it's signifiant. However, i don't use Keep-Alive myself, so perhaps i'm wrong. Try starting a thread on it, then we'll see ;)

McMohan

6:06 pm on May 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks Claus. You sure follow-up on threads you participated. More wait then, a newfound trait of a wannabie SEO :)

Just pondering, which would be the ideal forum to start a thread on this. Maybe Google news.

claus

6:17 pm on May 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Things Take Time ...yeah it seems there's more of that these days. In several different ways, even. Still, it's the same for the new as for the experienced, as far as i can tell.

As for forum:

The Apache forum would be the right one for "Keep-Alive". For "Things Take Time", well, in absense of a zen buddhism forum, perhaps Google News ... it will also take time to get the post through pre-moderation *lol*