...

View Full Version : Dynamic URL Rewrite



Ste9890
10-07-2010, 02:27 PM
Hi all, here is my issue:

I have a dynamic URL:
https://localhost/driver-courses2.php?action=show&id=5

I would like this URL to display as:
https://localhost/driver-courses2.php/page-name

Also, if possibe I would like this new extension to be dynamic, for example:
https://localhost/driver-courses2.php/$page-name

So the URL extension would depend on the id value.

I hope this makes sense, any help would be greatly appreciated. I am using XAMPP on a localhost, I have mod_rewrite enabled and Allowoveride set to All.

Thanks in advance.

Ste9890
10-08-2010, 11:55 AM
Anyone?

I really need this issue solving... thanks :)

This is my current URL link:

<a href=\"{$_SERVER['PHP_SELF']}?action=show&amp;id=$id\">$coursename - $coursecode</a>

abduraooft
10-08-2010, 12:18 PM
I would like this URL to display as:
https://localhost/driver-courses2.php/page-nameIf you are getting the contents from a DB, you'd need to add an extra column to save the page-name(and add an to index it), and modify your query to fetch the data using this page-name instead of the id

Ste9890
10-08-2010, 12:46 PM
Hi abduraooft, many thanks for your reply!

Let me explain how my script is working...

This is my url link:


<a href=\"{$_SERVER['PHP_SELF']}?action=show&amp;id=$id\">$coursename - $coursecode</a>

The 'action' part calls this switch statement:


switch($_GET['action']) {

case 'show':
displayOneItem($_GET['id']);
break;
default:
displayCourses();
}


The 'action=show' part calls the displayOneItem function.

So how do I remove the 'action=show' from my URL and still be able to call the displayOneItem function?

Also, in your reply where you state "you'd need to add an extra column to save the page-name(and add an to index it)" - what do I need to add to 'index it'?

Thanks for your help.

abduraooft
10-08-2010, 01:17 PM
The technique that you need to adopt here is mod_rewrite, see an example at http://www.workingwith.me.uk/articles/scripting/mod_rewrite

Ste9890
10-08-2010, 03:53 PM
Thanks abduraooft.

I had come to the conclusion that I would need to use mod_rewrite orginally but cannot quite figure out how to make it work in the way I'd like it to, hence me seeking for help on here.

Can you shed any light as to what I need to do?

As I stated in my first post I have mod_rewrite enabled and Allow Override set to all. I also noted that I am using XAMPP incase that makes a difference.

This is the URL I'd like to rewrite:
https://localhost/driver-courses2.php?action=show&id=5

I would like it to show as:
https://localhost/driver-courses2.php/coursename

Any help would be greatly appreciated. I really need to get this solved. Thanjs.

abduraooft
10-08-2010, 04:10 PM
I would like it to show as:
https://localhost/driver-courses2.php/coursename


"<a href=\"/driver-courses2.php/$coursename\">$coursename - $coursecode</a>" ?

Ste9890
10-08-2010, 04:21 PM
But then how do I call the displayOneItem() function with the switch statement?

Also, that method meant all the external files (CSS etc.) weren't found.

abduraooft
10-08-2010, 05:00 PM
But then how do I call the displayOneItem() function with the switch statement? Have you got a basic mode_rewrite setup working at your end?

Ste9890
10-13-2010, 09:52 AM
I've read so much about it but still struggling to get my head around how it works.

I've got this at the moment:
RewriteRule ^course(.*)$ /driver-courses2.php?action=show&amp;id=$1 [L]

I can only get it to work with localhost/course&id=5 which means the URL still contains an ampersand and id=number.

Any chance you could help? It would be greatly appreciated. Cheers.

Ste9890
10-15-2010, 12:09 PM
Hi abduraooft,

At last I've managed to get it working how I needed.

I used this really useful mod_rewrite generator: http://www.generateit.net/mod-rewrite/ which was just what I needed!

Thanks for all your help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum