如果成功重写父目录,则只重写目录

时间:2013-11-07 10:07:44

标签: php regex apache .htaccess mod-rewrite

说我有这个网址:

http://mywebsite.com/forum/thread/129

我想改写为:

http://mywebsite.com/index.php?area=forum&page=thread&threadid=129

如何在不定义每个可能的子目录组合的情况下实现此目的?

这是我尝试过的:

RewriteRule ^(.*)/(.*)/(.*)$    index.php?area=$1&page=$2&threadid=$3   [NC,L]

以及其他一些尝试,都是徒劳的。

这就是我最终的结果(并且有效):

RewriteRule ^forum/?$                       index.php?area=forum                                [NC,L]
RewriteRule ^forum/thread/?$                index.php?area=forum&page=thread                [NC,L]
RewriteRule ^forum/thread/(.*)/?$       index.php?area=forum&page=thread&threadid=$1            [NC,L]

我怎样才能达到我想做的目的?

网址参数可以留空。我的剧本会忽略它们。

1 个答案:

答案 0 :(得分:1)

尝试此规则:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(?:/([^/]*)(?:/([^/]*))?)?/?$ /index.php?area=$1&page=$2&threadid=$3 [QSA,L]

认为不清楚你的意思是:Is there some regex that will only try and match the next sub-directory if it matches the first one?