2014-03-26 152 views
0

我想用php函数更改我的网站的根目录。用php更改站点根目录

这是我的网站(mysite.com)

本网站使用(当前根文件),并装载来自index.php这样

mysite.com/
    index.php 
    folder1 
    folder2 

现在我想,当我们打开(mysite.com)秀的index.php文件夹1的内部。(改变根/文件夹1 /)

我可以使用重定向,但我的地址会是这样mysite.com/folder1

但使用/folder1/作为根目录下有这个网站(mysite.com),我们希望用户我想(mysite.com/folder1

我应该怎么办?在index.php(使用folder1作为根目录)中使用的功能是什么?

+0

您将需要编辑您的'apache'服务器并指向根目录到'/ folder1'。如果你没有访问权限,你将不得不编辑你的'.htaccess'文件。 –

+1

你不要从PHP更改你的web服务器配置。事实上,你应该竭尽全力防止PHP修改web服务器配置(包括.htaccess文件)。否则你没有安全保障。 – symcbean

回答

2

你可以用你的.htaccess更改文件它默认加载:

# Set the startfile to something else: 
DirectoryIndex folder1/index.php 

改变基本的功能就是做*的傻事。这很令人困惑,特别是对于那些刚刚接触代码的人,或者如果你回顾一年左右的话。从长远来看,这将减缓进展和发展。 一个很大的不行!*

* 除非你知道你在做什么。你不是,如果你要问:)


如果你非常确定要与此穿过去,你可以设置DOCUMENT_ROOT到别的东西。

// Place this as high as possible in your code, like a config 
$_SERVER['DOCUMENT_ROOT'].= '/folder1'; 

为客户提供更专业的方法,你可以change the document_root in the httpd.conf,并建立新的价值服务器范围 PHP的$ _ SERVER值。

+0

我想php功能不htaccess ...在index.php中使用的php函数将根目录更改为/ folder1/files ... – user3464859

+0

@ user3464859另一种方法是在该目录中使用索引的“include”。否则,您可以查看路由。 – Ohgodwhy

+0

$ _SERVER ['DOCUMENT_ROOT']。='/ folder1';没有工作(http://donateload.tk/) – user3464859

4

这是使用的.htaccess一个非常适合,如果你不能访问httpd.conf文件。

RewriteEngine on 

# Change yourdomain.com to be your primary domain. 
RewriteCond %{HTTP_HOST} ^(www.)?yourprimarydomain.com$ 

# Change 'subfolder' to be the folder you will use for your primary domain. 
RewriteCond %{REQUEST_URI} !^/subfolder/ 

# Don't change this line. 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

# Change 'subfolder' to be the folder you will use for your primary domain. 
RewriteRule ^(.*)$ /subfolder/$1 

# Change yourdomain.com to be your primary domain again. 
# Change 'subfolder' to be the folder you will use for your primary domain 
# followed by/then the main file for your site, index.php, index.html, etc. 
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$ 
RewriteRule ^(/)?$ subfolder/index.php [L] 
+0

我会将我的声音添加到此问题的其他评论 - 如果可能,您应该通过Apache conf文件更改根目录。如果你无法做到,这是一个不错的选择。我甚至会将/ folder2中的所有内容都转移到根目录中。尽可能简化流程。 –

0

如果你真的想使用PHP,你可以根目录,将index.php文件在您与此内容:

<?php 
    header('Location: /folder1/'); 
?> 

Althought,更好的方式来做到这一点,是htaccess的。

+0

这将直接指向/ folder1!我不想直接使用!我想改变root不直接它 – user3464859

+0

然后,htaccess是唯一正确的方法。 – makallio85