一个url重写规则,适用于6个场景

时间:2022-11-24 08:22:13

I have a rewrite rule that 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 have 6 pages with similar scenario.

我有6页有类似的情况。

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

4 个解决方案

#1


2  

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:

编辑:我也在这里添加它,你可以通过检查/用一个删除2行规则?在它之后,它将匹配或不跟踪尾随/。解决方案将变为:

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

#2


2  

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]

#3


2  

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.

| (管道)用于正则表达式以提供替代方案,实际上它意味着“或”。您可以使用| 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.

如果您知道自己在做什么,也可以使用^([a-zA-Z] +)/?$,但这意味着您的规则会捕获仅包含英文字母的每个值。你必须处理错误。

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

我通过添加/来简化你的规则?最后,这意味着可选的结束斜线。

#4


0  

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]

#1


2  

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:

编辑:我也在这里添加它,你可以通过检查/用一个删除2行规则?在它之后,它将匹配或不跟踪尾随/。解决方案将变为:

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

#2


2  

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]

#3


2  

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.

| (管道)用于正则表达式以提供替代方案,实际上它意味着“或”。您可以使用| 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.

如果您知道自己在做什么,也可以使用^([a-zA-Z] +)/?$,但这意味着您的规则会捕获仅包含英文字母的每个值。你必须处理错误。

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

我通过添加/来简化你的规则?最后,这意味着可选的结束斜线。

#4


0  

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]