views:

128

answers:

8

Hello,

Consider the URL www.something.com/HereWeGo. When someone enters the url, the site shows a 404 error since the directory does not exists. I can parse the URL to get the query and HereWeGo, but cannot stop it from going to a 404 error.

How can I achieve it by PHP, if it is possible to avoid .htaccess now

Thanks Jean

+1  A: 

You could use mod_rewrite for that specific folder, or you could use your own custom 404 page (read this article).

fire
There is no folder as such
Jean
A: 

IIRC, setting up custom 404 (and other web server errors) are part of your webserver environment.

Alan
A: 

You can't avoid a .htaccess redirect unless you change your apache (or other webservers) configuration to redirect your 404 errors to your own *.php file instead of the default error message.

Kolky
+4  A: 

You will need to rewrite your URLs so /something directs to index.php?q=/something, the thing is your web server usually throws a 404 by default if the file / directory cannot be found and there are no RewriteRules associated with it, even if you can parse the URL from the error page and find the information you need.

Is there a valid reason why you can't use .htaccess? I would strongly suggest you use it if you can. Not least because at the moment a 404 will mean Google will not index your page.

This article will help you.

ILMV
+1  A: 

this is something that you need to configure on the webserver, in a .htaccess file. Take a look at the syntax for the redirect or rewrite. It isn't possible to do this in PHP alone as it is the webserver which processes the URL request.

ninesided
A: 

You have to setup a php file, such as 404.php as your 404 error handler file. After doing this have code in 404.php that looks something like this:

if (stristr($_SERVER["REQUEST URI"],"HereWeGo")
{
//add handling code here
}
Chris
assuming any radom for HereWeGo
Jean
+2  A: 

You just can't avoid "touching" .htaccess, at least for rewriting all requests to single file ( or defining default 404 file ). You are using Apache, not PHP for the server, right?

Take Kohana 3 Framework for example ( and most other frameworks ), all requests are rewritten to index.php file, which then handles the routes for the request.

RewriteRule .* index.php [L]

Code below will handle everything that responds with a 404 and redirect it.

ErrorDocument 404 /somefile.php

In the somefile.php you can then check if referer is 'local' ( $_SERVER['HTTP_REFERER'] ) and which query string it was about, responding with whatever you want.

Kemo
A: 

On your destination page simply call--

header('HTTP/1.0 200 OK');

--to override the default 404 status code.

haigek