我正在寻找一种方法将文件从根文件夹/
移动到指定文件夹/newFolder
,同时对用户隐藏此更改,例如,当转到
http://site.com/ABC
我想让浏览器显示
中的内容http://site.com/customerPages/ABC
,但客户端将在浏览器中看到以下URL:
http://site.com/ABC
我试过使用RewriteRule
,但由于文件夹名称不是预定义的,我没有这样做。然后我尝试使用file_get_contents()
在我的404页面,但它打破了所有的相对路径。
最后,我在404页面中使用了以下代码:
// Redirect customers to customers landing pages under /newFolder
if (strpos($message,'newFolder') === false) {
$url = 'http://'.$_SERVER['HTTP_HOST'].'/newFolder'.$message;
echo '<frameset><frame src="/newFolder'.$message.'"></frameset>';
exit();
} else {
// correct the URL - remove the `/newFolder` bit
$message = str_replace('/newFolder','',$message);
}
$message -请求的URI
我知道这个解决方案很脏,我想改进它。有人知道怎么做吗?
我的设计有什么问题吗?使用frameset
显示网页有什么问题吗?编辑
我最终保留了我最初的解决方案(使用404页面和框架集),以避免为所有页面创建规则,并使.htaccess
更重。
如果你想为你的客户端使用不同的URL
你可以在htaccess
中使用这个代码Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)/ABC/(.*?)/(.*)
RewriteRule ^ customerPages/%3 [L]
我测试它在我的Web主机工作正确
样本:虚拟URL
http://sepidarcms.ir/ABC/Client1/image/logo.png http://sepidarcms.ir/ABC/Client2/image/logo.png http://sepidarcms.ir/ABC/Client3/image/logo.png真实URL:
http://sepidarcms.ir/customerPages/image/logo.png实际上,我从来没有使用过cakePHP,但它为我工作;
文件/文件夹;
./
index.php
images/
so-logo.png
newfolder/
abc.php
内容;
// .htaccess
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ newfolder/$1.php [L]
// index.php
<? echo __file__; ?>
<br>
<img src="images/so-logo.png" width="100">
// abc.php
<? echo __file__; ?>
URL的输出;
// index.php - http://localhost/rewrite/
D:'LAMP'www'rewrite'index.php
[logo]
// abc.php - http://localhost/rewrite/abc
D:'LAMP'www'rewrite'newfolder'abc.php