zag Posted May 28, 2012 Posted May 28, 2012 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. http://www.testshop/product.php?id=278645 But how do I do this? 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
JJanisch Posted May 28, 2012 Posted May 28, 2012 This is done with a .htaccess file. Create a file called .htaccess on your root(index) directory and paste this: 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 1
JJanisch Posted May 28, 2012 Posted May 28, 2012 ..obviously change "page=" to your location, and ^view/ means http://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.
Marci Posted May 28, 2012 Posted May 28, 2012 With mod_rewrite installed & enabled in Apache, drop a .htaccess file in site root: 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 1
JJanisch Posted May 28, 2012 Posted May 28, 2012 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.
zag Posted May 28, 2012 Author Posted May 28, 2012 (edited) I'm using IIS, is there an equivalent? Also how would I do it like this? http://www.testshop.com/278645/This_is_product_Description Edited May 28, 2012 by zag
JJanisch Posted May 28, 2012 Posted May 28, 2012 See mod_rewrite - Apache HTTP Server & URL Rewriting for Beginners - Added Bytes - Brighton Web Application Development That addedbytes website is fantastic. I'll read through for better understanding!
Marci Posted May 28, 2012 Posted May 28, 2012 I'm using IIS, is there an equivalent? See Creating Rewrite Rules for the URL Rewrite Module : URL Rewrite Module : Installing and Configuring IIS : The Official Microsoft IIS Site 2
zag Posted June 15, 2012 Author Posted June 15, 2012 Thanks for all the help people I managed to get it working perfectly with the inbuild IIS7 Rewrite module I changed http://www.mysite.com/artist.php?i=1234 to http://www.mysite.com/artist/1234 ^Artist/([0-9]+)/?$ 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.
JJanisch Posted June 15, 2012 Posted June 15, 2012 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)!
tom_newton Posted June 15, 2012 Posted June 15, 2012 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/09/23/short-list-of-restful-api-frameworks-for-php/
zag Posted June 18, 2012 Author Posted June 18, 2012 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.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now