Jump to content

URL Rewrite for Clothes Store


htorbov

Recommended Posts

I want to make the links to my pages to looks from this:

 

1. CATEGORIES - http://daytona.bg/category?id=2&collection=04-2012 // opens category.php and tell to show all categories from collection 04-2012 with parentcat (parent category) - 2
2. PRODUCTS - http://daytona.bg/browse?id=22&collection=04-2012 // opens browse.php and tell to show all products from collection 04-2012 with category - 22
3. PRODUCT - http://daytona.bg/model?id=1286 // opens model.php and tell to show the product with ID 1286

 

To something like that:

 

1. CATEGORIES - http://daytona.bg/for-him/04-2012
2. PRODUCTS - http://daytona.bg/men-shirts/04-2012
3. PRODUCT - http://daytona.bg/men-lastic-shirt-silk-ysl-1286

 

Do someone have some suggestions how it can be done?

 

EDIT: At the moment I have this in my .htaccess:

### OPTIONS ###
Options +FollowSymLinks -Indexes -MultiViews
AddDefaultCharset UTF-8

### RENGINE ###
RewriteEngine On
RewriteBase /

### CONDITIONS ###
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

### ERROR DOCS ###
ErrorDocument 404 /404

### REWRITES ###
RewriteRule ^([^\.]+)$ /$1.php [NC,L]

Link to comment
Share on other sites

This will be a lot more doable if you rewrite to:

 

1. CATEGORIES - http://daytona.bg/category/for-him/04-2012
2. PRODUCTS - http://daytona.bg/browse/men-shirts/04-2012
3. PRODUCT - http://daytona.bg/model/men-lastic-shirt-silk-ysl-1286

 

There are actually 2 problems you have to deal with:

 

1. Rewriting the url to parameters

2. Adding the capability of your script to accept a slug, and lookup the proper id's in the database.

 

#2 has nothing to do with rewriting, and rewriting will not help you with that problem.  If your script is designed only to take a category id, then you have to add code to instead take a string, and lookup the category using that string rather than the id number.

 

 

 

Link to comment
Share on other sites

Yep, I guess the basic problem would be the url generation and detection, right?

Last night I was thinking about something like that..

 

URL Generation:

function friendlyURL($string) {
$string = preg_replace("`\[.*\]`U", "", $string);
$string = preg_replace('`&(amp;)?#?[a-z0-9]+;`i', '-', $string);
$string = htmlspecialchars($string, ENT_COMPAT, 'utf-8');
$string = preg_replace("`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i", "\\1", $string);
$string = preg_replace(array("`[^a-z0-9]`i", "`[-]+`"), "-", $string);
return strtolower(trim($string, '-'));
}

 

URL Detection for PRODUCT:

$url = $_SERVER['REQUEST_URI'];
$last_uri = end(explode('_', $url));
$productid = current(explode('.', $last_uri));

 

.htaccess Rules:

RewriteRule ^([^/]+) /model.php?id=$1 [NC]

 

When I come back home I'll think about it  :shrug:

Link to comment
Share on other sites

You are on the right track.  What many people do is store a "slug" in the database as an alternative to using the id.  If you do some googling on "sluggable" and "slug" you'll find material on it.  For the most part, you want to have it be a database call and not something you lookup, however, often people will use something like your friendly url, and just add on the id at the end of the slug.  So your category #20 might be "Clothes for him" and your url would be:

 

.../category/clothes_for_him_20/04-2012

 

And your code simply needs to strip off the digits after the last '_' in the string.

 

As for your rewrite rules... almost there, but you of course need to have one for each pattern you want to match, so rather than have a completely generic one you have one for /category, /browse and /model.  For example the category rule (assuming you use a slug instead of an id) might be:

 

RewriteRule ^/category/([a-zA-z0-9_-]+)/(.*)$  category.php?cat=$1&collection=$2

 

 

Link to comment
Share on other sites

I made it with this way:

 

.htaccess Rules:

RewriteRule ^category/([^/]+)/([^/]+) /category.php?collection=$1 [NC]
RewriteRule ^browse/([^/]+)/([^/]+) /browse.php?collection=$1 [NC]
RewriteRule ^model/([^/]+) /model.php [NC]

 

Recognizing:

MODEL:
$product = current(explode('.', end(explode('_', $_SERVER['REQUEST_URI']))));

CATEGORY:
$category = current(explode('.', end(explode('_', $_SERVER['REQUEST_URI']))));
$collection = htmlspecialchars($_GET["collection"], ENT_QUOTES);

BROWSE:
$browse = current(explode('.', end(explode('_', $_SERVER['REQUEST_URI']))));
$collection = htmlspecialchars($_GET["collection"], ENT_QUOTES);

 

Functions:

function friendlyURL($string) {
$string = preg_replace("`\[.*\]`U", "", $string);
$string = preg_replace('`&(amp;)?#?[a-z0-9]+;`i', '-', $string);
$string = htmlspecialchars($string, ENT_COMPAT, 'utf-8');
$string = preg_replace("`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i", "\\1", $string);
$string = preg_replace(array("`[^a-z0-9]`i", "`[-]+`"), "-", $string);
return strtolower(trim($string, '-'));
}

 

Usage:

echo <a href="<?=friendlyURL($product_name);?>">$product_name</a>

 

Is it okay? :-)

Link to comment
Share on other sites

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.