Forum Moderators: phranque
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!
It seems like you've done it almost right. Let's take one thing at a time. Rewrites first.
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.
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.
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?
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.
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.
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.
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)
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 ;)
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*