htaccess - 类别/子类别/页

时间:2016-12-11 20:20:27

标签: apache .htaccess parameters subquery categories

我有一个小问题:

我的网址:localhost / cat / Category / Subcategory / Page

示例:localhost / cat / movies / 3d / 7

守则:

RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/?$ $1.php?wq=$2 [NC,L]  ## wq is Category
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ $1.php?wq=$2&wsq=$3 [NC,L]  ## wsq is Subcategory / but if no Subcategory and Pagination -> False it must be "&wpq="
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ $1.php?wq=$2&wsq=$3&wpq=$4 [NC,L]   ## wpq is Pagination

我目前的htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-z0-9\-]+)/?$ $1.php [NC,L]

RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/?$ $1.php?wq=$2 [NC,L]  ## wq is Category
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ $1.php?wq=$2&wsq=$3 [NC,L]  ## wsq is Subcategory / but if no Subcategory and Pagination -> False it must be "&wpq="
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ $1.php?wq=$2&wsq=$3&wpq=$4 [NC,L]   ## wpq is Pagination

RewriteCond %{REQUEST_METHOD} !POST [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([\w.-]+)/?$ $1.php [L]

尽管只能输入一个类别(并且没有子类),但只有问题才能解决问题,那么应该检查参数2是否仅为数字。

如果仅输入数字,那么它不是子类别而是分页参数(那么第二个参数必须是" wsq"而不是" wsq")。

我希望你理解我的意思:D

这可以用Php完成,但我认为通过.htaccess会更好吗?

已经提前感谢你了。

1 个答案:

答案 0 :(得分:1)

如果我得到它,请替换:

RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ $1.php?wq=$2&wsq=$3 [NC,L]

使用:

RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/(\d+)/?$ $1.php?wq=$2&wpq=$3 [NC,L]
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ $1.php?wq=$2&wsq=$3 [NC,L]

(\d+)(相当于([0-9]+)([[:digit:]]+))匹配一个数字。这个新规则更具体,需要在之前定位您的“常规”规则(需要字母和/或数字和/或破折号)。