Ronald LaPlante
Ronald LaPlante

Reputation: 33

Dynamic 404 Page.. kinda

Alright, so this is what I am trying to achieve.

I want to be able to send URL data to a php file if the server returns 404.

for example:

example.com/stackoverflow 

would bring up a dynamic 404 page

(404.php?id=stackoverflow)

but I am not using it for 404 in my case, I want to send the data after the domain.com/

So that I can pull from my database and display content accordingly.

I know this can be done with a small rewrite in the .htaccess, regex is just confusing for me.

I do NOT want a redirect.

http://example.com/datahere

should show the data of

404.php?id=datahere

Upvotes: 2

Views: 155

Answers (1)

Amit Verma
Amit Verma

Reputation: 41219

You can use ErrorDocument directive to redirect 404 uris to /404.php something like the following :

ErrorDocument 404 /404.php?id=%{REQUEST_URI}

This will rewrite /datahere to /404.php?id=datahere ( /datahere will show you the contents of /404.php?id ) .

Note that the above directive doesn't work on apache versions bellow 2.4 as the mod-rewrite variable %{REQUEST_URI} is treated as plain text not as a variable on lower versions.

On apache 2.2 you could use mod-rewrite to rewrite non-existent requests to 404 . Use the following :

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /404.php?id=$1 [L]

Upvotes: 1

Related Questions