Content > Web Design

Cleaning up PHP $_GET URLs

(1/4) > >>

yny-u:
I am building a site with PHP/MySQL, and I was wondering.. Is there a good way to get URLs like

/gallery/index.php?section=illustration&id=27

to appear as, and and be accessible from something like

/gallery/illustration/tree-27

where "tree-27" is the name of the picture and its id. (Having the id number in the URL along with the name prevents to images with identical names from causing problems..)

Desh:
The way I know of is pretty easy, and I actually learned it here from Databits in this topic :D

Basically, you take your php script that reads your values and chop off the .php.  Then, using .htaccess, you force the webserver to run it as a php script.  After that, you make your script read its URL, breaking it up by the slashes, and you have your variables.

In actual code, for the .htaccess


--- Code: ---<Files ScriptName>
  ForceType application/x-httpd-php
</Files>

--- End code ---
(Where ScriptName is the name of the php script you cut off)

Then, to get your variables, in the ScriptName script


--- Code: ---$request=str_replace($_SERVER['SCRIPT_NAME'],'',$_SERVER['REQUEST_URI']);
$variables=explode('/',$request);

--- End code ---

That first line will get rid of the ScriptName, and leave only your variables.  Then, your variables are broken up by slashes into an array.

Keep in mind, the way your script gets is variables is now different.  In your original method, variables are named and part of the $_GET group.  In this method, they're numbered starting at 0 and part of the $variables array. 

If you name your variables right away, such as $id=$_GET['id'];, just replace the $_GET with the right $variables[ #].  If you use $_GET throughout your script, you'll have to go through and change them.

Be sure to avoid naming a folder the same as ScriptName.  If you do, when you access ScriptName/Variable/Variable, the server will look for Variable/Variable in the folder and ignore your script.

Xepher:
Another option is to use ModRewrite to silently redirect the requests within apache itself.

This goes inside a .htaccess file placed at the base of your website (/home/username/public_html/.htaccess)

RewriteRule ^/gallery/(.*)/(.*)-(.*) /gallery/index.php?section=$1&id=$3


You may have to tweak the exact paths (the path it sees depends on where you put the .htacvess file, and I'm typing this from memory) but that's generally how it works. The $1 and $2 bits in the second part are replaced by the first and second parenthetical matches in the first part. The ".*" is a regular expression term meaning match-any-character (.) any-number-of-times (*). You can fine tune that using more specific regular expressions... you may also need to tweak the first part to be  "^/gallery/(.*)/(.*)-(.*)(/?)" in order to catch cases where people (or their browsers) end the URL with a final slash (/).

The advantage to doing it this way is you don't have to tweak your script in anyway, so it's easy to use with pre-made scripts. The only downside is you have to figure out regular expressions to do it. :-) Hopefully I've done the hard bit for you though.

Databits:
Or you can do a third option where you direct ALL of the requests for your site through a single script which internally outputs what it needs to after parsing the information out.

yny-u:
Ahh, thank you all very much for your help. I shall mess around with this..

Thanks again.

Navigation

[0] Message Index

[#] Next page

Go to full version