用php更改站点根目录

I want to change the root of my website with php functions.

This is my website (mysite.com)

This website using (current root document) and loading from index.php like this

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/ )

I can use redirect but my address will be like this mysite.com/folder1

but I want this website (mysite.com) using /folder1/ as root directory with out we want to user (mysite.com/folder1)

What should I do? What is the functions for this to use in index.php (to use folder1 as root directory)?

You can use your .htaccess to change the file it default loads:

# 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.

// 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.

This is an excellent fit for .htaccess usage if you can't access the 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]

If you really want use php, you can place index.php file in you root dir with this content:

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

Althought, better way to do this, is with htaccess.