2012-12-22 85 views
1

我想将一个文件夹的所有无文件和无目录重定向到另一个文件夹。 .htacess应该位于root(http://site.com)上。htacess - 将一个文件夹重定向到另一个内部

例子:

http://site.com/admin/core/file.php - Does not exists. So, redirect (internaly) to http://site.com/adminhide/core/file.php 

http://site.com/admin/index.php - Exists. So, don't do anything.. 

我试图创建的代码,但我有一个500内部服务器错误:

Options +FollowSymLinks

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !^/admin/$ 
RewriteRule (.*)$ /adminhide/$1 [L] 

请帮助我, 谢谢您的时间:)

回答

0

你说:

http://site.com/admin/index.php - Exists. So, don't do anything.. 

这使在admin路径中存在的文件,但在同一时间你的规则将拒绝该路径下的所有文件:

RewriteCond %{REQUEST_URI} !^/admin/$` 

在另一方面,no-files and no-directories真的是含糊不清,留下任何与工作,但它似乎指的是non-existent文件。如果是这样,映射所有404错误到http://site.com/adminhide/core/file.php将完成你想要的。

试试这个,而不是你的问题设置的重写规则:

ErrorDocument 404 adminhide/core/file.php 

修订

你想要什么在PHP中做得更好。

由于404错误,必须先检测,重定向到htaccess的全球404脚本中放置这样的事情:

ErrorDocument 404 path/Error404.php 

在Error404.php插入像这样的代码:

// Check the address with error  
if ($_SERVER['REQUEST_URI'] == '/admin/core/file.php') { 

// Set the target error script accordingly 
$TargetURL = "http://site.com/adminhide/core/file.php"; 
} 

// Another option 
if ($_SERVER['REQUEST_URI'] == '/whatever/file.php') { 
$TargetURL = "http://site.com/whatever/file.php"; 
} 
... // More options 

// Go to appropriate file 
echo '<script type="text/javascript">window.location ="' . $TargetURL . '";</script>'; 
+0

嘿。不,我希望它是动态的。所以,如果不是/admin/core/file.php是/admin/core/ola.php,它将内部重定向到/adminhide/core/ola.php。与文件index.php相同。我不想不重定向只有index.php文件,但是/ admin /目录中的所有现有文件。谢谢 –

+0

@William更新了我的答案。希望是有用的。 –

+0

您好,我希望它在内部重定向,因此当您输入site.com/admin/core/file.php时,因为它不存在,它会向您显示site.com/adminhide/core/file.php,但地址栏会保留。谢谢。 –

相关问题