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 ...
28th May 2012, 08:40 AM #1
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.
But how do I do this?
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
28th May 2012, 08:48 AM #2
This is done with a .htaccess file. Create a file called .htaccess on your root(index) directory and paste this:
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
28th May 2012, 08:51 AM #3
..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.
28th May 2012, 08:51 AM #4
With mod_rewrite installed & enabled in Apache, drop a .htaccess file in site root:
(I think the code above is correct off the top of my head)
RewriteRule ^/(\d+)$ /product.php?id=$1
See mod_rewrite - Apache HTTP Server & URL Rewriting for Beginners - Added Bytes - Brighton Web Application Development
28th May 2012, 08:53 AM #5
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.
28th May 2012, 09:06 AM #6
I'm using IIS, is there an equivalent?
Also how would I do it like this?
Last edited by zag; 28th May 2012 at 09:21 AM.
28th May 2012, 09:10 AM #7
That addedbytes website is fantastic. I'll read through for better understanding!
Originally Posted by Marci
28th May 2012, 09:14 AM #8
2 Thanks to Marci:
JJanisch (29th May 2012), zag (15th June 2012)
15th June 2012, 12:23 PM #9
Thanks for all the help people
I managed to get it working perfectly with the inbuild IIS7 Rewrite module
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.
15th June 2012, 02:23 PM #10
This all depends on the file location. This caught me out at first too.
Originally Posted by zag
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)!
15th June 2012, 02:31 PM #11
That type of query is usually called "REST" - and is often created with some sort of framework rather than "raw" PHP.
18th June 2012, 08:48 AM #12
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.
By mbird in forum Hardware
Last Post: 4th January 2014, 04:34 AM
By DaveP in forum General Chat
Last Post: 17th January 2010, 08:18 PM
Last Post: 30th July 2009, 02:36 PM
By mullet_man in forum Windows Server 2000/2003
Last Post: 6th March 2009, 12:21 PM
By Oops_my_bad in forum General Chat
Last Post: 2nd October 2007, 08:14 PM
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)