I am trying to setup my 404 page to detect the referring url. I would like to this in PHP any ideas? thank you very much
3 Answers
<?php
$referer = $_SERVER['HTTP_REFERER'];
?>
Note: Don't rely too much on this data as easily can be faked.
In your apache2.conf (or .htaccess)
ErrorDocument 404 /your_error_dir/your_error_file.php
Update:
http://ex.am/c8N4j
Your mod_rewrite should detect the given parameter: c8N4j
Your PHP script should search the given parameter in your database and if it's found it should redirect user to the actual page.
-
I think the question is how to setup Apache so that php code can be executed in his 404 page (which by default is just a static 404.htm page). – Mischa Sep 21 '10 at 11:40
-
sorry I should have given more details. I would like to setup a url shortening system. and i am going about doing it by creating shortcut that can be detected in my 404 page ( i am assuming that it can run php just like any other page on the server). So what I need is the refering url so I can decipher it to redirect to the right URL so a link should look like index/123456 ...since such link doesnt exist the user would be rerouted to the 404 page which should read that link and translate to the corresponding page. I might be going completely the wrong way but this is the idea so far. thanks – salmane Sep 21 '10 at 11:55
-
Sorry i don't understand your logic. Your shortcut (ideally) has nothing to do with 404. – fabrik Sep 21 '10 at 12:08
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
Now, all your requests will be redirected to index.php
The requested uri segment will be available as $_SERVER['REQUEST_URI']
.
So just take that and look up that string in your db and do whatever you need to do with that.
Example: I set up a test folder in my localhost environment, added the .htaccess and created a new index.php
<?php
$uri = $_SERVER['REQUEST_URI'];
$segments = explode('/', $uri);
var_dump($segments['2']);
Sent your browser to: http://localhost/test/123/foo and it will print
string '123' (length=3)
Now you have your string in $segments['2']. Use that in your db query. As easy as that.

- 1,739
- 2
- 11
- 25
-
Err, if you put it in subfolder test, you should of course change that in the .htaccess as well. Just sayin.. – Tilman Koester Sep 21 '10 at 13:21
-
thank you very much for the detailed example. Just a quick question with regards to regex in this case . what I have so far is general, how do I turn the following : RewriteRule ^entry/([^/\.]+)/?$ entry/?id=$1 [L] to something that takes only integers. that folder should have other files in it but I want the mod_rewrite to work only for integer entires. Thank you – salmane Sep 21 '10 at 14:59
-
The two rewrite conditions mean, to only execute the rewrite rule if neither a file nor directory with that name exist. So calling /add.php for example will call it normally. In short: Whenever you call anything, that doesnt exist, it will get sent to index.php. I would advise to check if an entry exists in the db for that value and if it doesnt redirect to some 404 page. I would also strongly recommend to run the value through mysql_real_escape_string() before putting it in a query. – Tilman Koester Sep 21 '10 at 15:32
did you mean?
$_SERVER['HTTP_REFERER']
The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.it cannot really be trusted.
Take a look also at Determining Referer in PHP