Получить параметры в любом порядке - PullRequest
0 голосов
/ 27 марта 2012

Для сайта с такими URL-адресами:

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 голосов
/ 27 марта 2012

Вот решение:

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

Мой любимый инструмент для проверки регулярных выражений:

http://www.quanetic.com/Regex (не забудьте выбрать ereg (POSIX) вместо preg (PCRE)!)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...