1

i know this has probably been answered before but i really don't know how to word this.

basically, i'm trying to store images, but when retrieving them i'd like the url to look something like http://www.example.com/xdfg485324

I know mvc .net does this with routes and controls and stuff, but is there a way to do that with php?

i was thinking that maybe since that xdfg485324 directory was not found, i would have a php script run and check to see if that image exists in the database then return the actual image data only (i'm not storing the actual image data in the database though, although i could)

As far as i know there's not really actually a good reason to do this, i just thought it looked more professional.

EDIT:

Thanks to Rasclatt getting me on the right track and providing some htaccess code, i was able to do exactly what i wanted, and the solution was something like this:

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^image/(.*)$ /test/image/?image=$1 [NC,QSA,L]

my actual url looks something like this "http://localhost/test/image/235443" i have an index.php in the image directory which gets called after this url rewrite.

images/index.php

<?php
if(isset($_GET['image']))
{
    $filename = "../uploadedimages/".$_GET['image'].".jpg";
    if(file_exists($filename))
    {
        header('Content-Type: image/jpeg');
        $image = imagecreatefromjpeg($filename);
        imagejpeg($image);
        imagedestroy($image);
    }
}
?>

all uploaded files are saved as jpg into the /test/uploadedimages directory, so after this script is ran, it returns the image requested from the uploadedimages directory, but the url still stays the same, even if you get the url to the image.

iedoc
  • 2,266
  • 3
  • 30
  • 53
  • 1
    If you are using an apache server, using a `.htaccess` file is probably the best way with a rewrite rule because if it is not a correct address, I think by default your server would just do an error `404`. If you have a rewrite, you can least force every hit to go to one file for routing using php. – Rasclatt Jul 02 '15 at 23:08
  • you know thats actually something that crossed my mind, wasn't sure if you have to have a 404 land on an html page or not, but i guess that doesn't make sense, should have looked into that further before i asked. thanks though, i'd mark this as an answer if you want to make it an answer, its a solution to my question – iedoc Jul 02 '15 at 23:13
  • https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/ – iedoc Jul 02 '15 at 23:41
  • I think that is the same article I looked at when I first was trying to figure out `.htaccess` rewrites, oddly enough. – Rasclatt Jul 02 '15 at 23:42

1 Answers1

2

As stated in my comment, if you are using an apache server, using a .htaccess file is probably the best way with a rewrite rule because if it is not a correct address, I think by default your server would just do an error 404. If you have a rewrite, you can least force every hit to go to one file for routing using php. Here is a basic example of this rewriting. You put this file in any folder you want it to have an affect. It will affect all child folders as well:

.htaccess

RewriteEngine On
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [NC,QSA,L]

Here are a couple of relevant StackOverflow references:

.htaccess rewrite to redirect root URL to subdirectory

How to enable mod_rewrite for Apache 2.2

How to remove index.php from URLs?

Community
  • 1
  • 1
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
  • my wamp server didn't like the RewriteEngine command, "Invalid command 'RewriteCond'", but that could just be because i'm on windows. This answers my question though! – iedoc Jul 02 '15 at 23:24
  • 1
    Windows uses a different kind. I think it's similar to xml – Rasclatt Jul 02 '15 at 23:24
  • I think it's a `web.config` file – Rasclatt Jul 02 '15 at 23:25
  • oh, web config is for .net projects in iis, maybe in apache too, not sure, but i'm able to use a .htaccess file and redirect a 404, just trying to keep the url data in the redirect though – iedoc Jul 02 '15 at 23:26
  • ah, i just had to enable the apache rewrite_module – iedoc Jul 02 '15 at 23:28
  • Oh yeah, you are right. `web.config` is non-apache windows. – Rasclatt Jul 02 '15 at 23:28
  • haha, i was just scrounging the net looking for why i couldn't use the rewritecond and you have that right in your post, thanks man! – iedoc Jul 02 '15 at 23:30