Php 在Apache中移动页面,同时删除相同的URL
我想重新排列我的网站,并将所有客户页面移动到一个目录(/customerPages),同时保持相同的URL(访问页面的URL和浏览器中显示的URL)。我使用的是Php 在Apache中移动页面,同时删除相同的URL,php,apache,url,cakephp,mod-rewrite,Php,Apache,Url,Cakephp,Mod Rewrite,我想重新排列我的网站,并将所有客户页面移动到一个目录(/customerPages),同时保持相同的URL(访问页面的URL和浏览器中显示的URL)。我使用的是Apache和PHP(CakePHP) 我已尝试按以下方式重新连接404错误页面: // Redirect customers to customers landing pages under /customerPages if (strpos($message,'customerPages') == false) { $url
Apache
和PHP
(CakePHP
)
我已尝试按以下方式重新连接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会发生变化。我试着摆弄重写规则
,但运气不好
如何使用第一种、第二种或任何其他方法来实现这一点?您需要将图像请求从较新位置(/customerPages)重定向到旧路径。 您可以使用mod_rewrite apache模块将此类请求重定向回:
RewriteEngine on
RewriteRule ^/customerPages/(.*\.jpg|.*\.gif|.*\.png) /oldpath/$1 [R]
另一种方式,只是基本想法: 1.放入/oldpath/.htaccess文件(我们处理未找到的文件404错误): 二,/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');
}
它不起作用。我已将带有html和php文件的图像复制到
/customerPages
,因此重定向应将/customerPages
添加到图像请求中,但我如何知道应将customerPages
添加到哪些图像?大约有300个页面,我不想将它们全部写为重写规则
,因此我不会减慢apache服务器的速度。因此,您需要从旧位置重定向到新位置:有许多onld位置,我希望使用一条规则捕获它们。我已经为此更新了我的问题,您必须为所有资源(页面、图像等)指定重写规则。您可以使用模式来减少重写规则行的数量。它将略微降低apache在旧路径中的速度(在其中使用.htaccess),但这是一个临时解决方案,而您需要修复指向新位置的所有链接。您的旧目录中是否有其他资源(页面、图像等)?文件_存在,因为它是URL,所以site.com/ABB
实际上是site.com/ABB/index.php
或site.com/ABB/index.html
。另外,我不明白它将如何在客户端浏览器中编辑URL?如果(文件_存在('/newpath/'.$filename)&(!is_dir('/newpath/'.$filename))){…}
k,我得到了这个信息。我可以为URL重写做些什么?根本不使用URL重写。那么我如何确保客户端将看到旧的URL呢?
$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');
}