我需要一个可以像这样的网址的.htaccess文件:
http://example.com/api/module/something/something/something......
url 可以有 n 个段,模块是可变的,我需要完成的是将其重写为:
http://example.com/module/api/something/something/something......
目前,这是通过以下PHP代码完成的,但这不是一个优雅的解决方案。
$pieces = explode("/", $_SERVER['REQUEST_URI']);
$pieces = array_slice($pieces, 2, count($pieces)-2,false);
$module = $pieces[0];
unset($pieces[0]);
$end = implode('/', $pieces);
header('Location: /'.$module.'/api/'.$end);
尝试将其添加到文档根目录中的 htaccess 文件中
RewriteEngine On
RewriteRule ^api/([^/]+)/(.*)$ /$1/api/$2 [L]
如果您希望它像在 PHP 代码中一样实际重定向,请将L
替换为括号中的R=301
。