以任何顺序获取参数

时间:2012-03-27 10:13:40

标签: apache .htaccess mod-rewrite

鉴于网站包含以下网址:

http://example.com/catalogue
http://example.com/catalogue/category-20
http://example.com/catalogue/customer-5
http://example.com/catalogue/category-20/customer-5
http://example.com/catalogue/customer-5/category-20

...有一种合理的mod_rewrite方法可以将内部重定向到:

http://example.com/catalogue.php
http://example.com/catalogue.php?category_id=20
http://example.com/catalogue.php?customer_id=5
http://example.com/catalogue.php?category_id=20&customer_id=5
http://example.com/catalogue.php?customer_id=5&category_id=20

...

我的尝试让我无处可去:

RewriteRule ^(.*/)category-(\d+)(.*)$   $1$3?category_id=$2 [QSA]
RewriteRule ^(.*/)customer-(\d+)(.*)$   $1$3?customer_id=$2 [QSA]
RewriteRule ^catalogue/?$               catalogue.php [L,QSA]
# Request exceeded the limit of 10 internal redirects due to probable
# configuration error. Use 'LimitInternalRecursion' to increase the limit 
# if necessary

RewriteRule ^(.*/)category-(\d+)(.*)$   $1$3?category_id=$2 [QSA]
RewriteRule ^(.*/)customer-(\d+)(.*)$   $1$3?customer_id=$2 [QSA]
RewriteRule ^catalogue/?                catalogue.php [L,QSA]
# Matches everything that starts with /catalogue (not intended)

1 个答案:

答案 0 :(得分:0)

以下是解决方案:

RewriteRule ^/catalogue(.*)(/category-([0-9]+))(.*) \
  /catalogue$1$4?category_id=$3 [QSA,NC]
RewriteRule ^/catalogue(.*)(/customer-([0-9]+))(.*) \
  /catalogue$1$4?customer_id=$3 [QSA,NC]
RewriteRule ^/catalogue/?$ /catalogue.php [NC,QSA,L]

如果不起作用请尝试不使用 /

RewriteRule ^catalogue(.*)(/category-([0-9]+))(.*) \
  /catalogue$1$4?category_id=$3 [QSA,NC]
RewriteRule ^catalogue(.*)(/customer-([0-9]+))(.*) \
  /catalogue$1$4?customer_id=$3 [QSA,NC]
RewriteRule ^catalogue/?$ /catalogue.php [NC,QSA,L]

使用/catalogue/进行测试:

  1  (2) init rewrite engine with requested uri /catalogue/
  2  (3) applying pattern '^/catalogue/(category-([0-9]+))(.*)' to uri '/catalogue/' 
  3  (3) applying pattern '^/catalogue/(customer-([0-9]+))(.*)' to uri '/catalogue/' 
  4  (3) applying pattern '^/catalogue/?$' to uri '/catalogue/' 
  5  (2) rewrite '/catalogue/' -> '/catalogue.php'
  6  (2) local path result: /catalogue.php
  7  (2) prefixed with document_root to /home/olivier/Documents/website/catalogue.php
  8  (1) go-ahead with /home/olivier/Documents/website/catalogue.php [OK]

使用/catalogue/category-54进行测试:

  1 (2) init rewrite engine with requested uri /catalogue/category-54
  2 (3) applying pattern '^/catalogue(.*)(/category-([0-9]+))(.*)' to uri '/catalogue/category-54'
  3 (2) rewrite '/catalogue/category-54' -> '/catalogue?category_id=54'
  4 (3) split uri=/catalogue?category_id=54 -> uri=/catalogue, args=category_id=54
  5 (3) applying pattern '^/catalogue(.*)(/customer-([0-9]+))(.*)' to uri '/catalogue'
  6 (3) applying pattern '^/catalogue/?$' to uri '/catalogue'
  7 (2) rewrite '/catalogue' -> '/catalogue.php'
  8 (2) local path result: /catalogue.php
  9 (2) prefixed with document_root to /home/olivier/Documents/website/catalogue.php
 10 (1) go-ahead with /home/olivier/Documents/website/catalogue.php [OK]

使用/catalogue/customer-128进行测试:

  1 (2) init rewrite engine with requested uri /catalogue/customer-128
  2 (3) applying pattern '^/catalogue(.*)(/category-([0-9]+))(.*)' to uri '/catalogue/customer-128'
  3 (3) applying pattern '^/catalogue(.*)(/customer-([0-9]+))(.*)' to uri '/catalogue/customer-128'
  4 (2) rewrite '/catalogue/customer-128' -> '/catalogue?customer_id=128'
  5 (3) split uri=/catalogue?customer_id=128 -> uri=/catalogue, args=customer_id=128
  6 (3) applying pattern '^/catalogue/?$' to uri '/catalogue'
  7 (2) rewrite '/catalogue' -> '/catalogue.php'
  8 (2) local path result: /catalogue.php
  9 (2) prefixed with document_root to /home/olivier/Documents/website/catalogue.php
 10 (1) go-ahead with /home/olivier/Documents/website/catalogue.php [OK]

使用/catalogue/category-54/customer-128进行测试:

  1 (2) init rewrite engine with requested uri /catalogue/category-54/customer-128
  2 (3) applying pattern '^/catalogue(.*)(/category-([0-9]+))(.*)' to uri '/catalogue/category-54/customer-128'
  3 (2) rewrite '/catalogue/category-54/customer-128' -> '/catalogue/customer-128?category_id=54'
  4 (3) split uri=/catalogue/customer-128?category_id=54 -> uri=/catalogue/customer-128, args=category_id=54
  5 (3) applying pattern '^/catalogue(.*)(/customer-([0-9]+))(.*)' to uri '/catalogue/customer-128'
  6 (2) rewrite '/catalogue/customer-128' -> '/catalogue?customer_id=128'
  7 (3) split uri=/catalogue?customer_id=128 -> uri=/catalogue, args=customer_id=128&category_id=54
  8 (3) applying pattern '^/catalogue/?$' to uri '/catalogue'
  9 (2) rewrite '/catalogue' -> '/catalogue.php'
 10 (2) local path result: /catalogue.php
 11 (2) prefixed with document_root to /home/olivier/Documents/website/catalogue.php
 12 (1) go-ahead with /home/olivier/Documents/website/catalogue.php [OK]

使用/catalogue/customer-128/category-54进行测试:

  1 (2) init rewrite engine with requested uri /catalogue/customer-128/category-54
  2 (3) applying pattern '^/catalogue(.*)(/category-([0-9]+))(.*)' to uri '/catalogue/customer-128/category-54'
  3 (2) rewrite '/catalogue/customer-128/category-54' -> '/catalogue/customer-128?category_id=54'
  4 (3) split uri=/catalogue/customer-128?category_id=54 -> uri=/catalogue/customer-128, args=category_id=54
  5 (3) applying pattern '^/catalogue(.*)(/customer-([0-9]+))(.*)' to uri '/catalogue/customer-128'
  6 (2) rewrite '/catalogue/customer-128' -> '/catalogue?customer_id=128'
  7 (3) split uri=/catalogue?customer_id=128 -> uri=/catalogue, args=customer_id=128&category_id=54
  8 (3) applying pattern '^/catalogue/?$' to uri '/catalogue'
  9 (2) rewrite '/catalogue' -> '/catalogue.php'
 10 (2) local path result: /catalogue.php
 11 (2) prefixed with document_root to /home/olivier/Documents/website/catalogue.php
 12 (1) go-ahead with /home/olivier/Documents/website/catalogue.php [OK]

两个提示:

如果您在托管环境中(=如果它是您自己的服务器可以修改虚拟主机,不仅仅是.htaccess文件),尝试使用RewriteLog指令:它可以帮助您找出这些问题:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

我最喜欢检查regexp的工具:

http://www.quanetic.com/Regex(别忘了选择ereg(POSIX)而不是preg(PCRE)!)