2013-01-20 40 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

相关问题