2013-01-20 76 views
0

我想重新排列我的網站,並將所有客戶頁面移動到目錄(/ customerPages)中,同時保持相同的URL(訪問該頁面的URL和顯示的URL在瀏覽器中)。我正在使用ApachePHPCakePHP)。在Apache中移動頁面,同時跳轉相同的URL

我試着重新佈線我404錯誤頁面以下列方式:

// Redirect customers to customers landing pages under /customerPages 
if (strpos($message,'customerPages') == false) { 
    $url = 'http://'.$_SERVER['HTTP_HOST'].'/customerPages'.$message; 
    $homepage = file_get_contents($url); 
    echo $homepage; 
} 

但是這種解決方案打破了使用相對路徑編寫的所有圖像。

後來我嘗試使用重定向來代替:

if (strpos($message,'customerPages') == false) { 
    $url = 'http://'.$_SERVER['HTTP_HOST'].'/customerPages'.$message; 
    header("Location: ".$url); 
} 

但比URL變化。我試圖用RewriteRule擺弄,但沒有運氣。

如何使用第一,第二或任何其他方法實現此目的?

回答

1

的另一種方式,只是基本的思路: 1.把你的/oldpath/.htaccess文件(我們處理文件未找到404錯誤) :

ErrorDocument 404 /oldpath/redirect.php 

2. /oldpath/redirect.php文件(我們的處理程序) - 使重定向到新的路徑只有當文件在新位置存在:

$url = parse_url($_SERVER['REQUEST_URI']); 
$filename = basename($url['path']); 

if(file_exists('/newpath/'.$filename)) { 
    header('Location: /newpath/'.$filename); 
}else{ 
    header('Location: /your_real_404_handler'); 
} 
+0

file_exists由於是URL而不起作用,所以'site .com/ABB'實際上是'site.com/ABB/index.php'或'site.com/ABB/index.html'。同樣,我不明白它將如何編輯客戶端瀏覽器中的URL? – Kuf

+0

'if(file_exists('/ newpath /'.$ filename)&&(!is_dir('/ newpath /'.$ filename))){...}' – clover

+0

k,我可以爲URL重寫做什麼? – Kuf

1

您需要將來自較新位置(/ customerPages)的圖像請求重定向到舊路徑。 您可以使用mod_rewrite Apache模塊對這樣的請求重定向回:

RewriteEngine on 
RewriteRule ^/customerPages/(.*\.jpg|.*\.gif|.*\.png) /oldpath/$1 [R] 
+0

它不起作用。我已經將帶有html和php文件的圖像複製到'/ customerPages'中,因此重定向應該將'/ customerPages'添加到圖像請求中,但是我怎麼知道應該在哪個圖像中添加'customerPages'?大約有300頁,我不想把它們都寫成'RewriteRule',所以我不會減慢Apache服務器的速度。 – Kuf

+0

所以你需要從舊位置重定向到新位置: – clover

+0

有很多現場位置,我想用一條規則來捕捉它們。我已更新我的問題 – Kuf

相關問題