I am struggling to work out how to write a rewrite rule to work for multiple fields for a product catalog that i'm currently building.
my files are stored in a folder on url.com/catalog - and then i want to structure url's like the below;
url.com/catalog/CATEGORY
url.com/catalog/CATEGORY/SUB-CATEGORY
url.com/catalog/CATEGORY/SUB-CATEGORY/PRODUCT
Where the Category, Sub-Category or Product is the URI from the database they are stored. This will then send to;
page.php?cat=CATURI&subcat=SUBCATURI&product=PRODUCTURI
I have used multiple threads and tutorials and come up with the below;
RewriteRule ^(.*)/(.*)/(.*)/(.*)$ catalog/page.php?cat=$2&subcat=$3&product=$4
which doesn't work at all how i need it to as the sub cat and product don't always have to pass.
Any suggestions for this?
You can divide your url into elements : catalog, category, sub-category, product. Using this function:
function get_path_elements() {
if (!isset($_SERVER['PATH_INFO'])) { return null; }
$path_info = $_SERVER['PATH_INFO'];
$path_elements = explode('/', $path_info);
array_shift($path_elements);
return $path_elements;
}
The work after is quite similar to what you have done.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.