.htaccess重写规则 - 自定义结构

时间:2013-03-07 04:29:30

标签: .htaccess mod-rewrite rewrite

是否可以创建一个.htaccess规则,该规则将采用URL结构的中间位置,但恢复正常的REQUEST_URL(对不起我的可怕解释)

以此网址为例 /船/ 283 /管理/水

现在假设我按照URL结构保持层次结构标准化,减去ID(在这种情况下ID是287) - 所以实际的脚本位置是/boats/manage/water(.php)

但显然我不必为每个页面都有一个手动规则,因为这会变得乏味。

例如(每页我想避免的事情)。

RewriteRule ^boats/(\d+)/manage/water$ ./boats/manage/water.php?id=$1  
RewriteRule ^boats/(\d+)/manage/bacon$ ./boats/manage/bacon.php?id=$1  

毫无疑问,我可以在谷歌找到相关内容,但我无法提出合适的关键词..

非常感谢任何正确方向的帮助/推动:)

1 个答案:

答案 0 :(得分:1)

您可以尝试:

# group out the first path, the ID, then the rest
RewriteCond %{REQUEST_URI} ^/([^/]+)/(\d+)/(.*)$
# pre-check that the destination php file actually exists
RewriteCond %{DOCUMENT_ROOT}/%1/%3.php -f
# rewrite
RewriteRule ^ /%1/%3.php?id=%2 [L]
相关问题