【问题标题】:Change site root directory with php用php更改站点根目录
【发布时间】:2014-05-05 02:32:32
【问题描述】:

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

这是我的网站 (mysite.com)

此网站使用(当前根文档)并从 index.php 加载,如下所示

mysite.com /
  index.php
  folder1
  folder2

现在我想在我们打开 (mysite.com) 时在 folder1 中显示 index.php。(将根目录更改为 /folder1/ )

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

但我希望这个网站 (mysite.com) 使用 /folder1/ 作为根目录,而不是我们想要的用户 (mysite.com/folder1)

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

【问题讨论】:

  • 您将需要编辑您的apache 服务器并将根指向/folder1。如果您无权访问此文件,则必须编辑您的 .htaccess 文件。
  • 您不会从 PHP 更改您的网络服务器配置。事实上,您应该不遗余力地防止 PHP 修改网络服务器配置(包括 .htaccess 文件)。否则你没有安全感。

标签: php .htaccess


【解决方案1】:

您可以使用 .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 值设置为新值serverwide

【讨论】:

  • 我想要 php 函数而不是 htaccess ... 在 index.php 中使用 php 函数将根更改为 /folder1/files...
  • @user3464859 另一种方法是使用该目录中索引的include。否则,您可以查看路由。
【解决方案2】:

【讨论】:

    【解决方案3】:

    如果您无法访问 httpd.conf,这非常适合 .htaccess 使用。

    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]
    

    【讨论】:

    • 我会在这个问题上向其他 cmets 添加我的声音 - 如果可能,您应该通过 Apache conf 文件更改您的根目录。如果您不能这样做,这是一个不错的选择。我什至只是将 /folder2 中的所有内容移动到根目录中。尽可能简化流程。
    【解决方案4】:

    如果你真的想使用 php,你可以将 index.php 文件放在你的根目录中,内容如下:

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

    虽然,更好的方法是使用 htaccess。

    【讨论】:

    • 这将指向 /folder1 !!!我不想直接使用!!!我想改变根目录而不是直接它
    猜你喜欢
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 2020-10-13
    • 2010-09-24
    • 1970-01-01
    • 2016-05-27
    相关资源
    最近更新 更多