Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Oct 2010
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Dynamic URL Rewrite

    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.

  • #2
    New Coder
    Join Date
    Oct 2010
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Anyone?

    I really need this issue solving... thanks

    This is my current URL link:
    Code:
    <a href=\"{$_SERVER['PHP_SELF']}?action=show&amp;id=$id\">$coursename - $coursecode</a>

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    I would like this URL to display as:
    https://localhost/driver-courses2.php/page-name
    If 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
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    New Coder
    Join Date
    Oct 2010
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi abduraooft, many thanks for your reply!

    Let me explain how my script is working...

    This is my url link:

    Code:
    <a href=\"{$_SERVER['PHP_SELF']}?action=show&amp;id=$id\">$coursename - $coursecode</a>
    The 'action' part calls this switch statement:
    Code:
    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.

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    The technique that you need to adopt here is mod_rewrite, see an example at http://www.workingwith.me.uk/article...ng/mod_rewrite
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    New Coder
    Join Date
    Oct 2010
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    I would like it to show as:
    https://localhost/driver-courses2.php/coursename
    PHP Code:
    "<a href=\"/driver-courses2.php/$coursename\">$coursename - $coursecode</a>" 
    ?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #8
    New Coder
    Join Date
    Oct 2010
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #9
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    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?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #10
    New Coder
    Join Date
    Oct 2010
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #11
    New Coder
    Join Date
    Oct 2010
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •