.htaccess的URL重写的问题:如何区分上市和详细页 [英] Htaccess Rewriting URL issue: how to distinguish Listing and detail page

查看:105
本文介绍了.htaccess的URL重写的问题:如何区分上市和详细页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个电子商务网站,用户可以在任何类别发布项目(类别可用2〜4级)。

I am developing an commerce site, Where users can post items in any categories (categories can be 2 to 4 levels).

我要生成上市和详细信息页面网址:

I want to generate URL for listing and details pages:


  • 清单页面将显示在内部类的产品清单

  • 详细页面将显示在内部类别中的所有信息项
    (内蒙古类是指分层,即最后一类在 /分类/ autos4x4s /三菱/职业者/ 内平均枪骑兵)

  • Listing page will show list of items in inner category
  • Detail Page will show all information for item in inner category (Inner category means Last Category in hierarchic i.e. in /classified/autos4x4s/mitsubishi/lancer/ inner mean "lancer")

下面是链接我要生成


  1. /分类/ autos4x4s /三菱/职业者/ (上市为)

<$c$c>/classified/autos4x4s/mitsubishi/lancer/2011/3/12/lancer-2002-in-good-condition-14/ (查看详细)

我想重定向如果URL,如果6项被传递(4类别名称+ 2日期和名称),以detail.php存在只有4类别ads.php。

I want to redirect to ads.php if just 4 categories exist in URL and to detail.php if 6 items are passed (4 category name + 2 date and title).

我写这些规则:

#listing ads
RewriteRule ^(.*)/(.*)/(.*)/(.*)/?$ ads.php?c1=$1&c2=$2&c3=$3&c4=$4 [NC,L]

#Detail pages
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/?$ detail.php?c1=$1&c2=$2&c3=$3&c4=$4&dt=$5&at=$6 [NC,L]

但是,所有的网站页面重定向到ads.php(列表页)甚至主页。

But all the sites page redirect to ads.php (Listing page) even home page.

我改变了规则如下​​:(虽然我不想使用清单和详细的网址为什么我在一些网站上看到,因为我想开始: <$c$c>/classified/autos4x4s/mitsubishi/lancer/2011/3/12/lancer-2002-in-good-condition-14/)

I changes the rules as follow: (Even though I do not want to Use Listing and Detail in start of URL. Why as I see on some site as I want: /classified/autos4x4s/mitsubishi/lancer/2011/3/12/lancer-2002-in-good-condition-14/)

#Listing pages
RewriteRule ^Listing/(.*)/(.*)/(.*)/(.*)/?$ ads.php?c1=$1&c2=$2&c3=$3&c4=$4 [NC,L]

#Detail pages
RewriteRule ^Detail/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/?$ detail.php?c1=$1&c2=$2&c3=$3&c4=$4&dt=$5&at=$6 [NC,L]

现在所有其它页面都很好,但是当我通过 /分类/ autos4x4s /三菱/撰稿人/ 2011/3/12 /蓝瑟-2002-在良好的条件-14 / 它总是转到页列表(ads.php)不详细页面。

Now all other pages are fine, but when I pass /classified/autos4x4s/mitsubishi/lancer/2011/3/12/lancer-2002-in-good-condition-14/ it always goes to Listing page (ads.php) not to detail page.

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

请用正则表达式的部分注意:

Please watch out with the regex part:

(.*)/(.*)/(.*)/(.*)/(.*)/(.*)

该点会匹配'/'太多,因此对于某些URL这将导致可能匹配的combinatoric爆炸,使得它非常低效重写。请考虑写这部分就像任何一种:

The dot will match a '/' too, so for certain urls this leads to a combinatoric explosion of possible matches, making it very inefficient to rewrite. Please consider writing this part like any of these:

(\w+)/(\w+)/(\w+*)/(\w+*)/(\w+*)/(\w+*)
([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)

您的问题是与此相关的还有:含有6/字符的URL会被你的4倍匹配斜线图案太(因为匹配/和)

Your question is related to this as well: a url containing 6 '/' characters will be matched by your 4 times slash pattern too (because . matches / as well)

您可以尝试:

#Listing pages
#e.g. /classified/autos4x4s/mitsubishi/lancer/
RewriteRule ^(\w+)/(\w+)/(\w+)/(\w+)/$ ads.php?c1=$1&c2=$2&c3=$3&c4=$4 [NC,L]

#Detail pages
#e.g. /classified/autos4x4s/mitsubishi/lancer/2011/3/12/lancer-2002-in-good-condition-14/ 
RewriteRule ^(\w+)/(\w+)/(\w+)/(\w+)/(\d+/\d+/\d+)/([^/]+)/$ detail.php?c1=$1&c2=$2&c3=$3&c4=$4&dt=$5&at=$6 [NC,L]

这篇关于.htaccess的URL重写的问题:如何区分上市和详细页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆