+ Post New Thread
Results 1 to 12 of 12
Web Development Thread, Using the URL for an ID field in Coding and Web Development; I'm just updating my PHP knowledge and have come up against a problem. I've always programmed in PHP, passing variables ...
  1. #1
    zag
    zag is offline
    zag's Avatar
    Join Date
    Mar 2007
    Posts
    3,829
    Thank Post
    918
    Thanked 422 Times in 355 Posts
    Blog Entries
    12
    Rep Power
    88

    Using the URL for an ID field

    I'm just updating my PHP knowledge and have come up against a problem.

    I've always programmed in PHP, passing variables to the php page like this.

    Code:
    http://www.testshop/product.php?id=278645
    But how do I do this?

    Code:
    http://www.testshop.com/278645
    I see most on-line sites now do it this way, but I have no idea what technique they are using. Some shops even have the description in the URL as well for better SEO. A Google search has been fruitless so far

  2. #2
    JJanisch's Avatar
    Join Date
    Nov 2011
    Location
    Swindon
    Posts
    178
    Thank Post
    17
    Thanked 17 Times in 17 Posts
    Rep Power
    8
    This is done with a .htaccess file. Create a file called .htaccess on your root(index) directory and paste this:

    Code:
    RewriteEngine on
    
    RewriteRule ^view/([^/\.]+)/?$ index.php?page=$1 [L]
    RewriteRule ^contact index.php?page=contact
    
    # 404 - Retains folder name, so /abcd displays 404.php content without forwarding to 404.php
    ErrorDocument 404 /404.php

  3. Thanks to JJanisch from:

    zag (28th May 2012)

  4. #3
    JJanisch's Avatar
    Join Date
    Nov 2011
    Location
    Swindon
    Posts
    178
    Thank Post
    17
    Thanked 17 Times in 17 Posts
    Rep Power
    8
    ..obviously change "page=" to your location, and ^view/ means www.website.com/view/testimonials.
    As seen below you can also create direct forwarders (contact page).

    Because you're typing in an exact file name; if the folder does not exist on your server then it will 404. The custom 404 page is done this way.

  5. #4
    Marci's Avatar
    Join Date
    Jun 2008
    Location
    Wakefield, West Yorkshire
    Posts
    895
    Thank Post
    84
    Thanked 235 Times in 194 Posts
    Rep Power
    82
    With mod_rewrite installed & enabled in Apache, drop a .htaccess file in site root:

    Code:
    RewriteEngine On 
    RewriteRule ^/(\d+)$ /product.php?id=$1
    (I think the code above is correct off the top of my head)

    See mod_rewrite - Apache HTTP Server & URL Rewriting for Beginners - Added Bytes - Brighton Web Application Development

  6. Thanks to Marci from:

    zag (28th May 2012)

  7. #5
    JJanisch's Avatar
    Join Date
    Nov 2011
    Location
    Swindon
    Posts
    178
    Thank Post
    17
    Thanked 17 Times in 17 Posts
    Rep Power
    8
    Also regarding 'description in the URL', as long as you call $_POST["id"], the following text (...-CandyBar-40p) I BELIEVE will be ignored. Don't trust me on this, test it. But I know it works in some way.

  8. #6
    zag
    zag is offline
    zag's Avatar
    Join Date
    Mar 2007
    Posts
    3,829
    Thank Post
    918
    Thanked 422 Times in 355 Posts
    Blog Entries
    12
    Rep Power
    88
    I'm using IIS, is there an equivalent?

    Also how would I do it like this?

    Code:
    http://www.testshop.com/278645/This_is_product_Description
    Last edited by zag; 28th May 2012 at 09:21 AM.

  9. #7
    JJanisch's Avatar
    Join Date
    Nov 2011
    Location
    Swindon
    Posts
    178
    Thank Post
    17
    Thanked 17 Times in 17 Posts
    Rep Power
    8
    That addedbytes website is fantastic. I'll read through for better understanding!

  10. #8
    Marci's Avatar
    Join Date
    Jun 2008
    Location
    Wakefield, West Yorkshire
    Posts
    895
    Thank Post
    84
    Thanked 235 Times in 194 Posts
    Rep Power
    82

  11. 2 Thanks to Marci:

    JJanisch (29th May 2012), zag (15th June 2012)

  12. #9
    zag
    zag is offline
    zag's Avatar
    Join Date
    Mar 2007
    Posts
    3,829
    Thank Post
    918
    Thanked 422 Times in 355 Posts
    Blog Entries
    12
    Rep Power
    88
    Thanks for all the help people

    I managed to get it working perfectly with the inbuild IIS7 Rewrite module

    I changed

    www.mysite.com/artist.php?i=1234

    to

    www.mysite.com/artist/1234

    Code:
    ^Artist/([0-9]+)/?$
    Code:
    artist.php?t={R:1}
    One thing I did notice that was all the relative links got broken. But I simple added a slash to the beginning of the image, link and css includes and it works again.

  13. #10
    JJanisch's Avatar
    Join Date
    Nov 2011
    Location
    Swindon
    Posts
    178
    Thank Post
    17
    Thanked 17 Times in 17 Posts
    Rep Power
    8
    Quote Originally Posted by zag View Post
    One thing I did notice that was all the relative links got broken. But I simple added a slash to the beginning of the image, link and css includes and it works again.
    This all depends on the file location. This caught me out at first too.

    Just remember that if you are in /include/hello/hello.html, you would normally include a file depending on the current location (say, notice.php in your root directory - /../../notice.php). Now you are using rewrite, your browser will think you are in a physical location (/hello/), and any links to other resources need to change accordingly. This will in hand cause problems for anyone who uses the ?artist.php?i=... option,so make sure none of your links are broken!

    The way I keep my code straight-forward is by using PHP's document_root, for example require($_SERVER["DOCUMENT_ROOT"]."/site/functions.php"). This way you are only working forward and not back!

    I know you're writing in ASP but this might be helpful (or to anyone else)!

  14. #11


    tom_newton's Avatar
    Join Date
    Sep 2006
    Location
    Leeds
    Posts
    4,475
    Thank Post
    866
    Thanked 850 Times in 672 Posts
    Rep Power
    196
    That type of query is usually called "REST" - and is often created with some sort of framework rather than "raw" PHP.

    http://blog.programmableweb.com/2011...works-for-php/

  15. #12
    zag
    zag is offline
    zag's Avatar
    Join Date
    Mar 2007
    Posts
    3,829
    Thank Post
    918
    Thanked 422 Times in 355 Posts
    Blog Entries
    12
    Rep Power
    88
    Yeh I was actually using PHP, works brilliantly.

    I've even done a name search function so I can get the entire artist name in the URL now. Works really well for search engines.

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 13
    Last Post: 4th January 2014, 04:34 AM
  2. Replies: 0
    Last Post: 17th January 2010, 08:18 PM
  3. Replies: 2
    Last Post: 30th July 2009, 02:36 PM
  4. Windows cannot access the file gpt.ini for GPO ID 1058
    By mullet_man in forum Windows Server 2000/2003
    Replies: 3
    Last Post: 6th March 2009, 12:21 PM
  5. looking for an LCD TV (for the kitchen - honest)
    By Oops_my_bad in forum General Chat
    Replies: 4
    Last Post: 2nd October 2007, 08:14 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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