Using link variables in an array to create clean URLs in PHP?

Hi Everyone…

I am a PHP newbie…please help me with this…
In my php script, I have written code to pull out the url variables and to store them in an array

For eg: if my url is www/viewgallery.php?cname=Colorado-Fall&pcaption=Light-On-Dunes

Then in my array ‘path_info’ I have

$path_info[base] = /
$path_info[query_var][cname] = Colorado-Fall
$path_info[query_var][pcaption] = Light-on-Dunes

etc

Now, How can I use this array to make clean urls in my php code?

If I try to write the link as

 <a href='viewgallery.php/{$path_info[query_var][cname]}/{$path_info[query_var][pcaption]}'></a>

it won’t take me to the requested page.

Ultimately, I want the url in my address bar to look as

www/viewgallery.php/Colorado-Fall/Light-On-Dunes

How can I make this link work with my php script? I know I can do this with .htaccess but is it possible to do it just with my php script using my array variables?

Thank you for your help…


$path_info['base'] = "/";
$path_info['query_var']['cname'] = "Colorado-Fall";
$path_info['query_var']['pcaption'] = "Light-on-Dunes";


$url = "www/viewgallery.php".$path_info['base'].$path_info['query_var']['cname'].$path_info['base'].$path_info['query_var']['pcaption'];


> [COLOR=#000000][FONT=Helvetica Neue]How can I make this link work with my php script? I know I can do this with .htaccess but is it possible to do it just with my php script using my array variables?

To a certain extent. Kind of.

The most PHP way still involves a bit of .htaccess stuff. You can’t do it without any .htaccess stuff, but you can do the actual requested url to accessed page mapping in PHP.

What you have to do is create a catch all PHP script – a page routing script. Then when someone accesses [/FONT][/COLOR]yourdomain.com/asdfasdf or [COLOR=#000000][FONT=Helvetica Neue]yourdomain.com/jhhjjh or any page on your server the .htaccess file maps to (rewrites to not redirects to) your page routing script. In your page routing script you ascertain if the url is an OK one, and if it is, you simply include() the right page from the server. Or include() the not found page.

So you need to use .htaccess a bit – to have all page requests funnel into your page routing script. But the mapping between the url the user requests and the page off the server they get can be done with PHP.

So you need to get a .htaccess rewrite rule which maps (rewrites not redirects) to your single page routing script (you’ll probably need to make sure some requests, eg for images and pdfs, aren’t rewritten). Once you’ve got that the more proper bit of rewriting takes place in PHP – simply by include()ing the right file from the server.[/FONT][/COLOR]

Thank you Johnyboy… :slight_smile:

So,does it mean,if index.php is the file containing page routing script, then in my .htaccess file I need to specify a rewrite rule to send all requests to index.php file?

> But the mapping between the url the user requests and the page off the server they get can be done with PHP.

How can I do this? I know how to parse and get the url variables the user requests but I don’t know how to map it. Could you help me with this?

Hi Ahmed… thank you for your time. I am able to create the link in the format I want but for some reason it won’t work. I am thinking that besides just creating the format I like to see, I need to somehow connect it with the actual link.

Yes. Apart from non-web-page requests, such as images.

> But the mapping between the url the user requests and the page off the server they get can be done with PHP.

How can I do this? I know how to parse and get the url variables the user requests but I don’t know how to map it. Could you help me with this?

$root = rtrim($_SERVER['DOCUMENT_ROOT'], '/');
include $root.'/whatever/path/to/your-file.php';

So you just use basic PHP logic to make up the path however you want, however it should be (I’m talking about the ‘/whatever/path/to/your-file.php’ bit - replace that with the right path on the server - this path remains entirely unknown/invisible to the user).

It does mess with a few server variables a bit though. So watch out for that. For example the $_SERVER[‘SCRIPT_NAME’] will be your page routing file, index.php say, for all pages/scripts.

Hi yathrakaaran,

Here’s how you could have a url like www.example.com/gallery/Colorado-Fall/Light-On-Dunes.

You need a .htaccess that looks something like this:


Options -MultiViews

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /
  RewriteRule ^gallery/([a-zA-Z-]+)/([a-zA-Z-]+)$ viewgallery.php?cname=$1&pcaption=$2 [QSA,L]
</IfModule>

You may or may not need the first line - I need it on my machine for the rewrite rules to work properly. Also, if your website is in a subfolder under the domain (e.g. www.example.com/yathrakaaran/) then you’ll need to change the RewriteBase setting (e.g. RewriteBase /yathrakaaran/).

This will accept cname and pcaption values of upper and lowercase letters and hypens, but nothing else.

Hi Johnyboy,
I am sorry… I think I should have mentioned it earlier that mine is a photo gallery and I am trying to make it work while pulling images from database in viewing the gallery. This being the case, can I send all the requests to my index.php file regardless of if it being a page or an image?

Also, in that case,I am assuming I cannot use include() as it is not a page but an image that I am pulling from the database based on the user click. Am I right?
Or is there a way around to make my link in the form index.php/Colorado-Fall/Light-On-Dunes to work?
Thanks again and sorry for not making myself clear in the first place…

Hi fretburner,
I am trying to do it in php with a minimal use of .htaccess if needed.
I have heard that in .htaccess file,we can redirect or rewrite all the url requests to a php routing file and then make the necessary mapping of the requested url and the actual url in the php file.
I am trying to figure out this idea and implement it. Do you have any suggestions?

Thanks

The usual way would be to do something like this in your .htaccess file:


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L] 

Note: you may have to remove the leading slash from index.php to get it work.

Then in index.php you can get the details of the requested URL from $_SERVER['REQUEST_URI']. You might want to look into using a router library (such as this one) to handle routing the requests and prevent you having to reinvent the wheel.

Hmm, I think it might be possible. But it’s starting to seem a bit extreme/silly. But I could be wrong. I’m pretty sure you could kind of pass an image through PHP as it were. Therefore use PHP as a url to file router, but it doesn’t seem, on the face of it, a very good idea. If the images are normal images, as in files on your server, then some .htaccess Rewrite stuff would seem by far the most appropriate.

I think there is a way to do what you’re asking but it doens’t seem a very good idea. The way you’d do it is, once you’ve ascertained which image file, urm, I don’t think include() would work, I’m not sure, but failing that, access the image data from the image file and output it from PHP. Also you’d need to output appropriate image (jpg or png) headers – because your PHP router file would be standing in for and kind of pretending to be an image file.

Just use .htaccess Rewrite etc., unless there’s some very good reason not to?