开发者

Mod Rewrite Optional Parameters

I have a fairly complex set of rew开发者_如何学运维rite rules to give my site pages pretty URLs. Right now to deal with paging of search results I'm using 2 rewrite rules:

RewriteRule ^search/([0-9]+)$ /cgi-bin/search.pl?page=$1 [NC,L,QSA]
RewriteRule ^search$ /cgi-bin/search.pl [NC,L,QSA]

These handle URLs such as:

http://example.com/search
http://example.com/search/2
http://example.com/search/1000

I'm wondering how to combine these into 1 rewrite rule so that the search.pl script is called correctly and only passed the page parameter if a page is specified. I know it's a pretty basic question but I can't seem to find the answer anywhere. Thanks for your help!


You could do something like this:

RewriteCond page=$2 ^page=.+|
RewriteRule ^search(/([0-9]+))?$ /cgi-bin/search.pl?%0 [NC,L,QSA]

But that’s not really nicer, is it?


Edit    Or if you’re fine with an empty page value:

RewriteRule ^search(/([0-9]+))?$ /cgi-bin/search.pl?page=$2 [NC,L,QSA]


You could use rewritemap with a small external script which maps non-empty to ?page=$1 and empty to empty. I would suggest sticking with what you've got.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜