简体   繁体   English

htaccess - 为 php 编写重写规则

[英]htaccess - writing a rewrite rule for php

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 上的文件夹中 - 然后我想像下面这样构建 url;

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.其中类别、子类别或产品是存储它们的数据库中的 URI。 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.之后的工作与您所做的非常相似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM