开发者

One url rewrite rule for 6 scenarios

I have a rewrite rule that works for my index page

RewriteRule ^index$ index.php开发者_Go百科 [L]
RewriteRule ^index/$ index.php [L]
RewriteRule ^index/page/(0?[1-9]|1[0-2])$ index.php?recordstart=$1 [L]
RewriteRule ^index/page/(0?[1-9]|1[0-2])/$ index.php?recordstart=$1 [L]

I have 6 pages with similar scenario.

eg.

RewriteRule ^graduate$ graduate.php [L]
RewriteRule ^graduate/$ graduate.php [L]
RewriteRule ^graduate/page/(0?[1-9]|1[0-2])$ graduate.php?recordstart=$1 [L]
RewriteRule ^graduate/page/(0?[1-9]|1[0-2])/$ graduate.php?recordstart=$1 [L]

I am looking for a rewrite that can match the six exact scenarios, instead of repeating the four lines six times.

Thanks


Something like:

RewriteRule ^(scenario1|scenario2|scenario3)$ $1.php [L]
RewriteRule ^(scenario1|scenario2|scenario3)/$ $1.php [L]
RewriteRule ^(scenario1|scenario2|scenario3)/page/(0?[1-9]|1[0-2])$ $1.php?recordstart=$2 [L]
RewriteRule ^(scenario1|scenario2|scenario3)/page/(0?[1-9]|1[0-2])/$ $1.php?recordstart=$2 [L]

Feels dirty, but if you want to exclude all other scenario's except the ones mentioned.

EDIT: I'll add it here too that you can remove 2 lines of rules by checking the / with an ? after it, it will match with or without the trailing /. The solution would become:

   RewriteRule ^(scenario1|scenario2|scenario3)/?$ $1.php [L]
   RewriteRule ^(scenario1|scenario2|scenario3)/page/(0?[1-9]|1[0-2])/?$ $1.php?recordstart=$2 [L]


First, just to save some space, you can shorten the first four rules to two like this:

RewriteRule ^index(/|)$ index.php [L]
RewriteRule ^index/page/(0?[1-9]|1[0-2])(/|)$ index.php?recordstart=$1 [L]

Then you can modify these two lines as follows:

RewriteRule ^(page1|page2|page3|page4|page5|page6)(/|)$ $1.php [L]
RewriteRule ^(page1|page2|page3|page4|page5|page6)/page/(0?[1-9]|1[0-2])(/|)$ $1.php?recordstart=$1 [L]


RewriteRule ^(index|graduate)/?$ $1.php [L]
RewriteRule ^(index|graduate)/page/(0?[1-9]|1[0-2])/?$ $1.php?recordstart=$2 [L]

| (pipe) is used in regex to provide alternatives, actually it means "or". You can add as many values as you want separated with |s.

If you know what you're doing you could also use ^([a-zA-Z]+)/?$ as well, but that would mean your rule captures every value that only consists of English letters. You must handle errors then.

I've simplified your rules by adding /? in the end which means an optional closing slash.


Does this work (untested)?

RewriteRule ^(index|graduate|...)$ $1.php [L]
RewriteRule ^(index|graduate|...)/$ $1.php [L]
RewriteRule ^(index|graduate|...)/page/(0?[1-9]|1[0-2])$ $1.php?recordstart=$2 [L]
RewriteRule ^(index|graduate|...)/page/(0?[1-9]|1[0-2])/$ $1.php?recordstart=$2 [L]

I think you can also reduce the rules to the following:

RewriteRule ^(index|graduate|...)/?$ $1.php [L]
RewriteRule ^(index|graduate|...)/page/(0?[1-9]|1[0-2])/?$ $1.php?recordstart=$2 [L]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜