2012-06-29 61 views
1

我需要的,可以採取一個url像這樣的.htaccess文件:阿帕奇國防部,重寫的.htaccess段交換

http://example.com/api/module/something/something/something...... 

URL可以有段氮量和模塊是可變的,我需要什麼完成是改寫爲:

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); 

回答

0

嘗試,如果你想讓它實際上重定向就像是在你的PHP代碼做你的文檔根

RewriteEngine On 
RewriteRule ^api/([^/]+)/(.*)$ /$1/api/$2 [L] 

加入這htaccess的文件中,在括號R=301更換L

+0

謝謝,請參閱我的編輯我忘了提及模塊是可變的。 – Stefan

+0

@stefan我編輯了重寫規則 –

+0

這並不完全是原創http://example.com/api/module/segment1/segment2 重寫 http://example.com/module/segment1/api/ segment2 – Stefan

相關問題