0

我有一系列重写来查看和处理消息中心:

RewriteRule ^messages/process/([a-zA-Z_-]+)/([0-9]+)/?$ messages-process.php?do=$1&id=$2 [L]
RewriteRule ^messages/process/ajax/([a-zA-Z_-]+)/?$ messages-process.php?do=$1&ajax=true [L]
RewriteRule ^messages/view/([0-9]+)/?$ messages.php?page=view&id=$1 [L]
RewriteRule ^messages/([a-zA-Z_-]+)/?$ messages.php?page=$1 [L]
RewriteRule ^messages/?$ messages.php [L]

我的问题是,我如何设置一个可以作为包罗万象的规则,所以如果有人浏览到/messages/process/他们仍然会在 messages-process.php 上结束,但没有设置所需的参数。

另一种看待它的方式是,我怎样才能使参数可选,重写仍然可以工作,但参数会丢失?

4

1 回答 1

1

您的请求/messages/process/与此规则匹配:

RewriteRule ^messages/([a-zA-Z_-]+)/?$ messages.php?page=$1 [L]

...因此您将需要一个更具体的规则,该规则/messages/process/更早匹配并将其路由到messages-process.php匹配更通用的规则之前。

将它放在所有其他规则之前将为该特定模式完成它:

RewriteRule ^messages/process/?$ messages-process.php [L]

如果您还有其他process/需要类似重写的目录,例如/messages/thingto ,请在其他目录之前messages-thing.php使用此规则。稍后您将需要明确列出不应被更通用规则匹配的那些...

# Matches /messages/process/ or /messages/otherdirectory/, etc...
RewriteRule ^messages/(process|thing|otherdirectory|somethingelse)/?$ messages-$1.php [L]

总之,这应该可以完成工作:

# Matches /messages/process/ or /messages/otherdirectory/, etc...
RewriteRule ^messages/(process|thing|otherdirectory|somethingelse)/?$ messages-$1.php [L]
RewriteRule ^messages/process/([a-zA-Z_-]+)/([0-9]+)/?$ messages-process.php?do=$1&id=$2 [L]
RewriteRule ^messages/process/ajax/([a-zA-Z_-]+)/?$ messages-process.php?do=$1&ajax=true [L]
RewriteRule ^messages/view/([0-9]+)/?$ messages.php?page=view&id=$1 [L]
# Stuff not matched by the specific rule at the top matches this one...
RewriteRule ^messages/([a-zA-Z_-]+)/?$ messages.php?page=$1 [L]
RewriteRule ^messages/?$ messages.php [L]

更新:

要允许/messages/process/trash一般重写 URL,您需要在此规则之后添加一条规则:

RewriteRule ^messages/process/([a-zA-Z_-]+)/([0-9]+)/?$ messages-process.php?do=$1&id=$2 [L]
# Match stuff after /process/whatever which are not followed by digits (which the previous rule matches)
RewriteRule ^messages/process/([a-zA-Z_-]+)(?:/[^0-9]+/?)$ messages-proces.php [L]
于 2013-03-07T15:58:32.193 回答