I've got a rewrite rule that actually works for my index page

RewriteRule ^index$ index.php [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've 6 pages concentrating on the same scenario.


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'm searching for a rewrite that may match the six exact situations, rather than repeating the 4 lines six occasions.


Something similar to:

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 when you need to exclude other scenario's except those pointed out.

EDIT: I'll add it here too that you could remove 2 lines of rules by checking the / by having an ? after it, it'll match without or with the trailing /. The answer 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]
RewriteRule ^(index|graduate)/?$ $1.php [L]
RewriteRule ^(index|graduate)/page/(0?[1-9]|1[0-2])/?$ $1.php?recordstart=$2 [L]

| (pipe) can be used in regex to supply options, really this means "or". You can include as numerous values as you would like separated with |s.

If guess what happens you are doing you might use ^([a-zA-Z]+)/?$ too, but that will mean your rule captures every value that only includes British letters. You have to handle errors then.

I have simplified your rules with the addition of /? ultimately meaning an optional closing slash.

First, to save some space, you are able to shorten the very first four rules to 2 such as this:

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

You'll be able to modify both of these lines the following:

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]

All of this (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 believe you may also lessen the rules towards the following:

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