Jump to content

Recommended Posts

I'm a complete newbie with mod-rewrite, but I've been programming php and javascript for many years. I have a client with a store that has 1,000+ products with more being added/changed daily. each product page is generated dynamically using php & mySQL and looks like this:

 

http://www.mysite.com/store/?Page=18&Product_ID=3001424

 

needless to say I don't want to put in mod_rewrite rules for every product independently. I don't even know where to start with this. it looks as if the mod_rewrite in .htaccess doesn't get generated automatically. so how do sites like my clients or even bigger sites deal with this?

 

ThnX

Link to comment
https://forums.phpfreaks.com/topic/282456-mod_rewrite-for-1000-products/
Share on other sites

You don't define a mod_rewrite rule for every product. You'd define your rule using regex. example rule

RewriteRule ^product/([a-z0-9_\-]+)/?$ view_product_page.php?product=$1

The above rule will match urls like

site.com/product/chocote-bar/
site.com/product/samsung_tvs/ 

... etc

 

mod_rewrite will map those urls to the following urls

site.com/view_product_page.php?product=chocolate-bar
site.com/view_product_page.php?product=samsung_tvs 

In view_product_page.php the superglobal variable $_GET['product'] will get the product from the url.

Ch0cu3r - 

 

ThnX for responding...

 

so I create the view_product_page.php page? what is in it? do I create a MySQL query that gives me the "chocolate-bar" string? 

 

it seems like there are 2 different places that the mod_rewrite has to act...

 

1) when someone clicks on a product link on the site and the URL that is displayed is the nice one like -

 

  site.com/view_product_page.php?product=chocolate-bar

 

and 

 

2) when someone gets that nice URL, clicks on it and gets taken to the correct product page

 

is this correct?

 

how does #2 happen? there is no /product/ folder? Page 18 is a page called productPage.php that's a page that does a MySQL query based on the passed ID and displays the formatted product information.

mod_rewrite maps fake urls to ones that do exist (your existing urls). products/ is not a folder it is just a unique identifier that is captured by mod_rewrite. Whats follows after products/ is sent to view_product_page. It sets the query string variable product to that value.

 

Create a file called test_page.php Now in add the following code to it

<?php

if(isset($_GET['message']))
    echo 'Message is: ' . $_GET['message'];

?>
<p>
   Try me out<br />
  <a href="/test_page.php?message=hello+world">existing url</a><br />
  <a href="/test/hello+world">clean url using mod_rewrite</a><br />
</p>

Now in an .htaccess file (that is full name of the file) add this to it. make sure its in same folder as test_page.php

RewriteEngine On

RewriteRule ^test/([a-z0-9_\-\+]+)/?$ test_page.php?message=$1 [NC,L]

Have a read of this article http://www.sitepoint.com/guide-url-rewriting/ it'll help you more than I can explain it.

Edited by Ch0cu3r
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.