简体   繁体   English

用php更改站点根目录

[英]Change site root directory with php

I want to change the root of my website with php functions. 我想使用php函数更改网站的根目录。

This is my website (mysite.com) 这是我的网站(mysite.com)

This website using (current root document) and loading from index.php like this 该网站使用(当前根文档)并像这样从index.php加载

mysite.com /
  index.php
  folder1
  folder2

now I want when we open (mysite.com) show index.php inside of folder1.(change the root to /folder1/ ) 现在我想当我们打开(mysite.com)在folder1内显示index.php时(将根目录更改为/ folder1 /)

I can use redirect but my address will be like this mysite.com/folder1 我可以使用重定向,但我的地址将类似于mysite.com/folder1

but I want this website ( mysite.com ) using /folder1/ as root directory with out we want to user ( mysite.com/folder1 ) 但是我希望这个网站( mysite.com )使用/folder1/作为根目录,而我们不想使用( mysite.com/folder1

What should I do? 我该怎么办? What is the functions for this to use in index.php (to use folder1 as root directory)? index.php中使用什么功能(将folder1用作根目录)?

This is an excellent fit for .htaccess usage if you can't access the httpd.conf. 如果您无法访问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]

You can use your .htaccess to change the file it default loads: 您可以使用.htaccess更改默认加载的文件:

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

Changing basic functionality is a stupid thing to do* . 更改基本功能是一件愚蠢的事* It's confusing, especially for people who are new to the code, or if you look back in a year or so. 这令人困惑,特别是对于刚接触该代码的人,或者如果您回顾一年左右。 It will slow down progress and development in the long run. 从长远来看,它将减慢进度和发展。 A big no go!* 大不能!*

* Unless you know what you are doing. * 除非您知道自己在做什么。 Which you're not if you have to ask :) 如果您要问的话,您不是哪个:)


If you are very sure you want to go through with this, you can set the DOCUMENT_ROOT to something else. 如果您确定要执行此操作,则可以将DOCUMENT_ROOT设置为其他内容。

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

To provide a more professional method, you could change the document_root in the httpd.conf , and have the php $_server value set to the new value serverwide . 为了提供更专业的方法,你可以更改在httpd.conf中的document_root ,并且已经设置了新值的服务器范围的PHP $ _ SERVER值。

If you really want use php, you can place index.php file in you root dir with this content: 如果您真的想使用php,则可以将index.php文件放置在根目录中,内容如下:

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

Althought, better way to do this, is with htaccess. 虽然,更好的方法是使用htaccess。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM