Edit php script to open URL in file

2019-08-27 18:59发布

问题:

I've just moved all my server and web apps over from apache to nginx.

I have one app which created short urls, it did this by create a 6 letter file and inside the file is simply the URL. I've managed to recreate the nginx rewrite rule so that it locates the file and opens its as php (instead of just downloading it as there is no .php extension on the files) however the script sees it as an invalid hash (as seen in the code below) is there anyway to simply this now just get my current links working, they all just contain a URL at the top, I want it to ignore the check and redirect to the URL irregardless.

Here is the script which is loading

<?php
if($_GET['id']){
    $id = addslashes($_GET['id']);
    if(file_exists("urls/$id"))
    { 
        $url = file_get_contents("urls/$id");
        header("location:".$url); 
    } else {
        echo "Error : invalid hash";
    }
} else {
    echo "Error : no hash";
}
?>

and it loads a file calling something like, for example "0ke0ea9ts" which will contain a url and nothing else.

I'm allowing the original links created (eg url.com/p/0ke0ea9ts) to work with the following Nginx redirect code (note they're installed in the sub-directory of /p/ hence the reference)

if (!-e $request_filename){
rewrite ^(.*)$ /p/show.php?id=$1 last;

so now as I said, it redirects opens them via the show.php but gives me the invalid hash message, how can I force it to just run the URL without the check?