【问题标题】:.htaccess mod-rewrite urls ending with "-XXXX" (dash, numbers).htaccess mod-rewrite url 以“-XXXX”结尾(破折号,数字)
【发布时间】:2018-03-20 19:40:28
【问题描述】:

我正在尝试匹配以破折号和一组数字(或只有一个数字)结尾但不起作用的网址:

示例:domain.com/my-product-36-inches-long-135355

# rewrite urls
Options +SymLinksIfOwnerMatch +FollowSymLinks -Indexes
RewriteEngine On
RewriteBase /


# product 
RewriteRule ^shop/-([0-9,]+)/?$ product.php?url_key=$1 [NC,L]

# category
RewriteRule ^shop/([^/.]+)$ category.php?url_key=$1 [NC,L]

# sub category
RewriteRule ^shop/([^/.]+)/([^/.]+)$ subcategory.php?url_key=$1&url_key2=$2 [NC,L]

# list (from subcategory page; will have 3 variables)
RewriteRule ^shop/([^/.]+)/([^/.]+)/([^/.]+)$ list.php?mr=3&url_key1=$1&url_key2=$2&url_key3=$3 [NC,L]

# list (from category page; will have 4 variables)
RewriteRule ^shop/([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)$ list.php?mr=4&url_key1=$1&url_key2=$2&url_key3=$3&url_key4=$4 [NC,L]



# business types
RewriteRule ^shop/restaurant-supply-business-type section.php?id=1 [NC,L]

# login
RewriteRule ^shop/customer/account/login login.php [NC,L]

# brands
RewriteRule ^shop/shopby/brand brands.php [NC,L]

# professional services
RewriteRule ^shop/professional-services professional_services.php [NC,L]

# pages 
RewriteRule ^locations/([^/.]+)$  page.php?url_key=locations/$1 [NC,L]
RewriteRule ^locations page.php?url_key=locations [NC,L]
RewriteRule ^testimonials page.php?url_key=testimonials [NC,L]
RewriteRule ^contact-us page.php?url_key=contact-us [NC,L]
RewriteRule ^equipment-checklist page.php?url_key=equipment-checklist [NC,L]
RewriteRule ^shipping page.php?url_key=shipping [NC,L]
RewriteRule ^returns page.php?url_key=returns [NC,L]
RewriteRule ^lease-financing page.php?url_key=lease-financing [NC,L]
RewriteRule ^news page.php?url_key=news [NC,L]
RewriteRule ^advice page.php?url_key=advice [NC,L]
RewriteRule ^about-us page.php?url_key=about-us [NC,L]
RewriteRule ^careers page.php?url_key=careers [NC,L]
RewriteRule ^privacy page.php?url_key=privacy [NC,L]
RewriteRule ^terms-of-use page.php?url_key=terms-of-use [NC,L]
RewriteRule ^page/([^/.]+)$ page.php?url_key=$1 [NC,L]
RewriteRule ^section/([^/.]+)$ section.php?url_key=$1 [NC,L]


# Remove all .php extensions without interfering with .js or .css.
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !pagespeed
RewriteRule ^([^.]+?)/?$ $1.php [L]

# Remove index from url.
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d

# RewriteRule ^([^.]+?)/?$ index.php?$1 [L,QSA]

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting friendly-url


    【解决方案1】:

    您可以使用此规则仅匹配结尾部分:

    RewriteRule ^shop/.*-([0-9,]+)/?$ product.php?url_key=$1 [NC,QSA,L]
    

    【讨论】:

    • 感谢您的回复,但我无法正常工作。我得到相同的404。这是一个不起作用的真实网址示例:/shop/fountain-jar-2-qt-22916
    • 更新它以显示全部。我没有商店目录,都在根目录下。我的问题是类别、子类别、列表和产品都必须使用/shop/。
    • 试过了,同样的。我认为它与我相信的#category 相同。我可以让#product 使用它:RewriteRule ^shop/product/([^/.]+)$ product.php?url_key=$1 [NC,L] 但我只需要它位于“/shop/”处,而 url 中没有 /product/。
    • 我相信对于/shop/fountain-jar-2-qt-22916,您希望实际的内部URI 是product.php?url_key=22916example.com/product.php?url_key=22916 工作正常吗?
    • 是的,但不是您展示的方式。它是 url_key,所以example.com/product.php?url_key=the-key-assigned-to-a-sku-in-db-22916 可以正常工作。
    【解决方案2】:

    如果要匹配包含(-数字或数字)的 uri 结尾,请仅使用以下内容:

    RewriteRule ^shop/(.*)(-([0-9]+)|([0-9]+))$  product.php?url_key=$2 [R,L,NE]
    

    如果(-数字或只有一位数字)使用以下内容:

    RewriteRule ^shop/(.*)(-([0-9]+)|\b[0-9]\b)$ product.php?url_key=$2 [R,L,NE]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多