【问题标题】:mod_rewrite ecommerce URL designmod_rewrite 电子商务 URL 设计
【发布时间】:2010-11-21 06:04:19
【问题描述】:

我对如何构建产品列表页面、产品页面和网页有疑问。

大致翻译成这样:

  1. /bags/nicebag.html = /product.php?product=nicebag&category=bags
  2. /nicebag.html = /product.php?product=nice_bag
  3. /bags = productlisting.php?&category=bags

问题是网页将与列表中的第 2 个网页共享相同的 URL 结构 /contact.html = page.php?page=contact

.htaccess 中没有单独列出的原因是网页可以有不同的名称。甚至同一个页面也可以有多种语言。

没有的原因。 1 和 2 没有结合,是因为有时我只想参考产品,因为它可以属于多个类别。

你建议什么样的结构?

.htaccess

# Mod rewrite enabled.
Options +FollowSymLinks
RewriteEngine on

# ---- Rules ----

# product.php (Search for category & product name)
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)\.html?$ product.php?prod_id=$2&cid=$1 [NC,L]

# productlisting.php (Search for category)
RewriteRule ^([A-Za-z0-9-_]+)?$ productlisting.php?&cid=$1 [NC,L]

【问题讨论】:

    标签: apache url .htaccess mod-rewrite


    【解决方案1】:

    几周前我遇到了同样的问题。 最终为“静态”页面定义了不同的结构。

    www.examples.com/contact/ 要么 www.examples.com/info/contact.html

    因此可以将其与“动态”页面区分开来。

    【讨论】:

    • 也是这么想的。但我觉得这样做有点笨拙。但也许我必须这样做:(
    【解决方案2】:

    如果不将非产品网页名称放入 .htaccess 或在接收 php 脚本中进行一些初步处理,几乎无法区分 www.examples.com/nicebag.html 和 www.examples.com/contact.html .

    如我所见,选项是:

    1. 重写所有对 page.php 的请求,对于那些不匹配任何非产品页面的请求,包括产品脚本

    2. 将非产品页面名称动态写入 .htaccess(混乱且容易出错)

    3. 重新考虑非产品页面的 URL 结构。也许只要 www.example.com/page/contact.html 可能会有所帮助

    无论如何,我会选择第三个。

    【讨论】:

      【解决方案3】:

      我会使用路径前缀/products/ 来标识与产品相关的URL。所以:

      • /products/bags/nicebag.html/product.php?product=nicebag&category=bags
      • /products/nicebag.html/product.php?product=nice_bag
      • /products/bags/productlisting.php?&category=bags

      使用这样的结构,您还可以将/products/ 重写为/productlisting.php,然后显示所有产品。

      # product listing
      RewriteRule ^products/$ productlisting.php [L]
      RewriteRule ^products/([A-Za-z0-9-_]+])$ productlisting.php?category=$1 [L]
      # product details
      RewriteRule ^products/([A-Za-z0-9-_]+)\.html$ product.php?prod_id=$1 [L]
      RewriteRule ^products/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)\.html$ product.php?prod_id=$2&cid=$1 [L]
      # other pages
      RewriteRule ^([A-Za-z0-9-_]+)\.html$ page.php?page=$1 [L]
      

      【讨论】:

      • 这个想法是避免在 URL 中使用“额外”关键字。因为这可能会对 Google 的搜索优化产生一些无法预料的负面影响。
      【解决方案4】:

      我会推荐扁平结构:

      • domain.com/bags
      • domain.com/contact
      • domain.com/nice-bag

      【讨论】:

        【解决方案5】:

        由于在 .htaccess 文件中维护重写规则会很麻烦且麻烦,所以我只会在其中放置一条规则,重写为:

        /dispatch.php?request=[request]
        

        例如

        RewriteRule ^(.*)$ dispatch.php?request=$1 [L,QSA]
        

        在 dispatch.php 中,您将请求分解为它的元素(路径、查询字符串、锚点,...)并决定从那里去哪里。这样,您就可以使用代码进行决策,这将为您提供比仅仅维护大量自定义重写映射列表更大的灵活性。

        例如,您可以通过查询数据库来识别路径中的产品和类别元素,并以更通用的方式将调度逻辑基于结果。

        [Pseudocode]
        if (isProduct($lastPathElement)) {
          // Maybe verify that leading path elements are categories ...
          // Other preparations/verifications ...
          // refer execution to product.php
        }
        elseif (isCategory($lastPathElement)) {
          // Other preparations/verifications ...
          // refer execution to productlisting.php
        }
        // ... (Checks for other specific stuff)
        else {
          // Static page or 404
          // refer execution to page.php
        }
        

        【讨论】:

        • 这个很不错!但是我现有的代码不允许这样做。我应该先从这个角度设计我的代码。
        【解决方案6】:

        为不同的类型使用不同的后缀,例如 html 用于产品,htm 用于页面或类似的东西

        /bags/nicebag.html = /product.php?product=nicebag&category=bags
        /nicebag.html = /product.php?product=nice_bag
        /bags = productlisting.php?&category=bags
        /contact.htm = page.php?page=contact
        

        /contact/page.html = page.php?page=contact
        

        【讨论】:

        • 这也是我的想法。我会同意的。
        猜你喜欢
        • 2019-07-03
        • 1970-01-01
        • 2018-09-03
        • 2016-12-06
        • 2021-05-26
        • 2020-01-28
        • 2013-10-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多