-2

I've worked with laravel earlier this year and I liked the idea of using url to access REST client. I was wondering how I'd be able to redirect from non-existant URL to an existing url and keeping that GET data.

Is it even possible with the current php implementation?

Thanks in advance!

EDIT: For example, let's say that I want to allow logged in users download a file. Said file would be downloaded from a following url: http://example.ex/image.png But if I just put my image.png into the root of my webserver, it'd be downloadable by anyone, as it's a direct link. How would I use the same URL, but to supply index.php with $_GET['url'] = image.png, so I could give a temporary download link to a user if the user is logged in.

MG lolenstine
  • 51
  • 2
  • 7
  • `Is it even possible with the current php implementation` Of course. But without any code it will be hard to provide an answer. – ArtisticPhoenix Sep 30 '18 at 06:26
  • I don't have any code yet, as I'm currently trying to figure out how I'd even start with that. Will edit my question with an example – MG lolenstine Sep 30 '18 at 06:27
  • To answer your question, yes it is possible. – Ibu Sep 30 '18 at 06:30
  • Your first step would be to look at [.htaccess](https://stackoverflow.com/questions/18406156/redirect-all-to-index-php-htaccess/38067042). – Ibu Sep 30 '18 at 06:39
  • @Ibu I'll do that, thanks. Can you change it into an answer, so I can tag it as answered? – MG lolenstine Sep 30 '18 at 06:42

1 Answers1

-2

You can do it like this (in PHP) I don't code Laravel

 $arr_url = parse_url('http://example.com/old_location?foo=bar');
 $query = !empty($arr_url['query']) ? '?'.$arr_url['query'] : '';

 header("Location: http://example.com/new_location{$query}");
 exit;

OR you can use $_SERVER ( I forget if it includes the ?)

   $query = !empty($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : '';
   header("Location: http://example.com/new_location{$query}");

Two important things.

  • no output before calling header not so much as a single space
  • call exit after using header.

from non-existant URL

As I said I don't use Laraval, so I can't speak on to how it handles 404's so you would need to do the redirection in the controller that handles that, which would require some logic to decide to redirect or not.

UPDATE

I want to do it in PHP, but this doesn't really help if my URL ending with image.png isn't a valid php file.

Saying access REST implies that it will be a PHP file. If you just mean finding if a file exists and redirecting then use HTACCESS

#if not is a real file
RewriteCond %{REQUEST_FILENAME} !-f
//redirect

How you decide where to redirect is an open question, as this is to broad for me to give a specific answer.

One last thing is your .png example. It's possible to use a .php file to return image data, with the correct headers. I once built a site that stored user images in a Zip file and then only displayed images if a user was logged in, by streaming the contents of the file from the ZIP (no temp files).

Everything the server returns is text, the only reason it's seen as an image is because Apache knows that .png should have Content-type: image/jpng or something like that for the content header. Something that can be "faked" in PHP.

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38