如何创建重写规则以将子目录转换为查询字符串


How to create rewrite rules to transform subdirectories into query strings

我想要这个:

www.example.com/query/bla/anotherquery/bla2/thirdquery/bla3

,并将其动态解释为:

www.example.com/index.php?query=bla&anotherquery=bla2&thirdquery=bla3

在httpd-vhost.conf中设置mod-rewrite函数

<VirtualHost *:80>
    DocumentRoot "/Users/me/Sites/Development"
    ServerName development
    RewriteEngine On
    RewriteRule ?
</VirtualHost>

我也希望http://www.example.com/index.php解析为http://www.example.com/

这可能吗?不安全吗?我该怎么做呢?我使用PHP和mac本地服务器与apache。

您可以在root .htaccess中使用此递归规则:

DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/([^/]*)(/.*)?$ $3?$1=$2 [L,QSA]