Вот решение:
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)!)