Off Topic

8 replies to this thread. Most Recent

atelier

24 Nov 2009, 9:59 am

How to write a .htaccess file correctly

Hi

Some time ago I asked my ISP to do the following: I wanted to have all 404 errors to be redirected to the index.html and return a 301 as if the ‘wrong’ page was moved to the index. (This because of a BIG makeover of the site. All the 250 pages removed and replaced with a CMS.) I found out what he did. He added in the .htaccess this code: <code>ErrorDocument 404 /404.htm</code> and in my 404.htm this: <code><? header( "Location: , True, 301 ); ?></code> Point is that I myself have no access to the .htaccess file and do not know the full code. (Yes, securety) Now my question. On another server I maintain some other websites. And there I do have access to that .htaccess file. Can someone please tell me what exactly must I put in that .htaccess file in order to have the same effect? I looked around and got a bit scared, because one can surely mess things up there. Thanks.

atelier

24 Nov 2009, 10:04 am

Hm, something went bezerk in the text on the web. Should be:

<code><? header( \"Location: http : // w w w .mysite.com / index . php \", True, 301 ); ?></code>\ Sorry about that.

waltd

25 Nov 2009, 2:16 pm

You have the entire recipe there. I would change the first line from 404.htm to 404.php, but that’s about it. The ErrorDocument part just goes by itself on a single line in the .htaccess file, and the PHP part is just as it is here, but don’t put the backslashes in before the quotes, they negate the quotes and your code won’t run.

Walter

Freeway user since 1997

http://www.walterdavisstudio.com

atelier

25 Nov 2009, 3:04 pm

Thank you Walter

Here’s som additional information I got recently, maybe it explains the back-slashes? ? My own ISP wrote me that in his .htaccess is this code

php_flag session.use_trans_sid off
php_flag register_globals off
<Files \".ht*\">
deny from all
</Files>

and in the server config is this:

<code>ErrorDocument 404 /404.htm</code>

Final question: Do you mean that I should rename also the filename 404.htm to 404.php?

And should I write in the .htaccess file on this other server only this (and rename the 404.htm to 404.php)

ErrorDocument 404 /404.php 

and put in the error.php this:

<? header( "Location: <http://www.mysite.com/index.php">, True, 301 ); ?>

I’m completely a noob when it comes to .htaccess.

Thanks!

waltd

25 Nov 2009, 4:01 pm

I don’t understand why your admin used backslashes in an .htaccess file, it doesn’t have to be there unless you are trying to actually act on a filename with a quotation mark in it.

Also, you don’t need the <code> thing in these list messages — adding the four tildes on either side of the code is the same as writing <pre><code> your code here </code></pre>. For one or two word examples inline, you can wrap a word or phrase with back-ticks foo() and get <code>foo()</code> back on the Web.

Walter

On Nov 25, 2009, at 11:04 AM, atelier wrote:

Thank you Walter

Here’s som additional information I got recently, maybe it explains the back-slashes? ? My own ISP wrote me that in his .htaccess is this code

php_flag session.use_trans_sid off
php_flag register_globals off
<Files \".ht*\">
deny from all
</Files>

and in the server config is this:

<code>ErrorDocument 404 /404.htm</code>

Final question: Do you mean that I should rename also the filename 404.htm to 404.php?

And should I write in the .htaccess file on this other server only this (and rename the 404.htm to 404.php)

ErrorDocument 404 /404.php

and put in the error.php this:

<? header( "Location: , True,
> 301 ); ?>

I’m completely a noob when it comes to .htaccess.

Thanks!

Freeway user since 1997

http://www.walterdavisstudio.com

atelier

25 Nov 2009, 4:23 pm

sorry about the <code>thingy</code> Walter, sometimes I just forget. It’s the weather, probably. Or age… ;-)

And it’s a yes to my previous post?

waltd

25 Nov 2009, 5:00 pm

On Nov 25, 2009, at 11:04 AM, atelier wrote:

Thank you Walter

Here’s som additional information I got recently, maybe it explains the back-slashes? ? My own ISP wrote me that in his .htaccess is this code

php_flag session.use_trans_sid off
php_flag register_globals off
<Files \".ht*\">
deny from all
</Files>

and in the server config is this:

<code>ErrorDocument 404 /404.htm</code>

Final question: Do you mean that I should rename also the filename 404.htm to 404.php?

And should I write in the .htaccess file on this other server only this (and rename the 404.htm to 404.php)

ErrorDocument 404 /404.php

I am not sure if you can set this in the .htaccess — by the time that file is read, it may be too late. Try it, but be prepared to put it at the httpd.conf level if it doesn’t work. Yes to the rename question.

and put in the error.php this:

<? header( "Location: , True,
> 301 ); ?>

Yes, this is correct.

I’m completely a noob when it comes to .htaccess.

Everyone begins somewhere.

Have a great Thanksgiving,

Walter

Freeway user since 1997

http://www.walterdavisstudio.com