Cleaning dirty URLs

wjh2303

New Member
Messages
139
Reaction score
0
Points
0
I was going to go for programming help, but since a search hadnt come up with anything i thought i'd ask this in tutorials and be optimistic that a reply would be worthy of such a description.

Currently, some of the URLs around my site are what i believe is described as 'dirty', i.e they look like:

and i would like to clean them up to look like
or, since the id is sufficient to uniquely identify some piece of content, and therefore which filename.php would be calling it, perhaps even

I've made a few queries on google and found references to mod_rewrite apache file, ASP, and pre-generating the page since it is dynamically generated but static in content (building the page from an SQL query which isnt going to change for a given query. However i am unfamiliar with fiddleing with apache, or ASP and cant readiy find anything that will get me started on how to do this.

TIA
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
You don't need to use mod_rewrite.
If you want to create the format like http://domain.com/filename/id then do this:
Create a PHP file called 'filename' without the .php extension. This will receive the request
Next, open the .htaccess file and insert this line:
Code:
<FilesMatch filename>
    ForceType application/x-httpd-php5
</FilesMatch>
This forces Apache to execute any requests for filename as PHP rather than look for the directory.
Finally, in your 'filename' file add the following code and modify it to do the correct thing with the received id:
PHP:
<?php
//get URL requested
$expl = explode("/",$HTTP_SERVER_VARS["REQUEST_URI"]);
//Find ID in URL
$id = intval($expl[count($expl)-1]);
//Check ID found, otherwise redirect to homepage
if(!$id){
header("location: /");
die();
}

//Do something with $id, probably not redirect like this though
header("location: /filename.php?id=" . $id);
?>
 

Hoobrum

Member
Messages
60
Reaction score
1
Points
8
Wow that looks like awesome code lemon-tree...I've gotta try that out sometime.
 
Top