Forum Moderators: coopster

Message Too Old, No Replies

htaccess custom 404 error help

passing vars to custom 404 page

         

indiandomain

10:32 am on Apr 11, 2003 (gmt 0)

10+ Year Member



need a little php help.
i want any html document on my server to goto a 404 page which takes it to /s.php3?term=

for eg mydomain.com/loans.html would show mydomain.com/s.php3?term=loans
how do i pass the variable 'term' in the .htaccess.

anyone can add a few php codes to pass the term variable.

ErrorDocument 404 /s.php3?term=

jatar_k

4:33 pm on Apr 12, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



anyone? ;)

DrDoc

4:41 pm on Apr 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php
$uri = $_SERVER['REQUEST_URI'];
$uri = urldecode($uri);
$uri = preg_replace("/ +/"," ",$uri);
$uri = trim($uri);
$uri = preg_replace("/^\/¦\/$¦\.(htm[l]?)/","",$uri);
header("Location: /s.php3?term=$uri");
?>

DrDoc

4:47 pm on Apr 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Note that this example doesn't take into account subdirectories or existing query strings...

/loans.html
/loans.htm
/loans/
/loans
...would all redirect to /s.php3?term=loans

However,
/loans.html?
/loans?
...would redirect to /s.php3?term=loans.html? and /s.php3?term=loans? respectively.

Let us know if you need any additional matching...

DrDoc

4:50 pm on Apr 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, if you change this line:
$uri = preg_replace("/^\/¦\/$¦\.(htm[l]?)/","",$uri);

to this:

$uri = preg_replace("/^\/¦[^a-zA-Z0-9].*/","",$uri);

Then it will strip the first non-alphanumeric character, and all following characters.

So,
/loans
/loans.html
/loans/
/loans.lkajshd ,.asdfkl;aj sdjas.fjk
...will all redirect to /s.php3?term=loans :)





Note: The pipe ¦ should not be broken, so you need to replace them manually

indiandomain

4:54 pm on Apr 12, 2003 (gmt 0)

10+ Year Member



can you please check if this syntax is correct.

will my .htaccess look like this

ErrorDocument 404 <?php
$uri = $_SERVER['REQUEST_URI'];
$uri = urldecode($uri);
$uri = preg_replace("/ +/"," ",$uri);
$uri = trim($uri);
$uri = preg_replace("/^\/¦\/$¦\.(htm[l]?)/","",$uri);
header("Location: /s.php3?term=$uri");
?>

DirectoryIndex index.php3
<Limit GET>
order allow,deny
allow from all
</Limit>

DrDoc

4:57 pm on Apr 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, in your .htaccess you should just refer to an error handling page.

ErrorDocument 404 /404.php3

Then, put that PHP code in a file you name 404.php3. That's the best and easiest way :)

DrDoc

5:01 pm on Apr 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want, you can even do this:

ErrorDocument 404 /s.php3

... and put this code first in your s.php3 file:

<?php
$uri = $_SERVER['REQUEST_URI'];
$uri = urldecode($uri);
$uri = preg_replace("/ +/"," ",$uri);
$uri = trim($uri);
$uri = preg_replace("/^\/¦[^a-zA-Z0-9].*/","",$uri);
$term = $uri;
?>

However, this could cause problems if your s.php3 file is ever accessed directly. If it only handles error requests, then it should be ok.

indiandomain

5:11 pm on Apr 12, 2003 (gmt 0)

10+ Year Member



dr one problem
im using this in 404.php3
<?php
$uri = $_SERVER['REQUEST_URI'];
$uri = urldecode($uri);
$uri = preg_replace("/ +/"," ",$uri);
$uri = trim($uri);
$uri = preg_replace("/^\/¦[^a-zA-Z0-9].*/","",$uri);
header("Location: /s.php3?term=$uri");
?>

site.com/loans.html goes to site.com/s.php3?term=/loans.html

instead of going /s.php3?term=loans

something wrong in the php code?

DrDoc

5:20 pm on Apr 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmmm..

Try replacing:
$uri = preg_replace("/^\/¦[^a-zA-Z0-9].*/","",$uri);

with:

$uri = preg_replace("/^\//","",$uri);
$uri = preg_replace("/[^a-zA-Z0-9].*$/","",$uri);

indiandomain

5:49 pm on Apr 12, 2003 (gmt 0)

10+ Year Member



man this is killer.
it works
:-)

1 more things doc.

in the browsers address bar i get /s.php?term=loans instead of /loans.html

any solution for this

DrDoc

6:06 pm on Apr 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, in your error document, change:

header("Location: /s.php3?term=$uri");

to:

$term = $uri;
include("/absolute/path/to/file/s.php3");

indiandomain

6:15 pm on Apr 12, 2003 (gmt 0)

10+ Year Member



doc thanks a million.
it worked.
:-)
now something like free_loans.html opens on /s.php3?term=free
how do i make it open at /s.php3?term=free+loans

DrDoc

6:31 pm on Apr 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Replace:

$uri = preg_replace("/[^a-zA-Z0-9].*$/","",$uri);

with:

$uri = preg_replace("/[^a-zA-Z0-9_].*$/","",$uri);
$uri = preg_replace("/_/","+",$uri);

Note that you don't need to pass the _ as a plus. You can replace the + in the above function with just a space. The variable would then be "free loans" instead of "free+loans". No urldecoding will be performed in s.php3 since the file is included

indiandomain

6:45 pm on Apr 12, 2003 (gmt 0)

10+ Year Member



doc
im very grateful to you and this board,
i got my problem sorted in a couple of hours.

cheers to you guys.
:-)

thanks a million.

-Sohail

DrDoc

7:07 pm on Apr 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're welcome :)

I'm glad we could help you.