假设一个站点的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)发布于 2012-03-27 18:52:04
以下是解决方案:
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)!)
https://stackoverflow.com/questions/9887513
复制相似问题