0

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

Cœur
  • 32,421
  • 21
  • 173
  • 232
  • You will need to either edit your `apache` server and point to root to `/folder1`. If you do not have access to this, you will have to edit your `.htaccess` file. – Patrick Reck Mar 26 '14 at 15:13
  • 1
    You don't change your webserver config from PHP. Indeed you should go to great lengths to *prevent* PHP from modifying the webserver config (including .htaccess files). Otherwise you have no security. – symcbean Mar 26 '14 at 15:20

4 Answers4

4

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]
Ohgodwhy
  • 44,803
  • 8
  • 64
  • 95
  • I'll add my voice to the other comments on this question - You should change your root through your Apache conf files if possible. This is a good option if you are not able to. I'd even just move everything from /folder2 into the root instead. Simplify the process as much as possible. – Mat Carlson Mar 26 '14 at 15:20
2

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.

Martijn
  • 14,522
  • 4
  • 29
  • 61
  • i want php functions not htaccess ... php function to use in index.php to change root to /folder1/files... – user3464859 Mar 26 '14 at 15:16
  • @user3464859 The alternative would be to use an `include` of the index within that directory. Otherwise, you can look into routing. – Ohgodwhy Mar 26 '14 at 15:17
0

Some other Q&As from SO that might help:

How do I change the root directory of an apache server?

how to remove folder name from url using htaccess

Community
  • 1
  • 1
logic-unit
  • 3,875
  • 11
  • 44
  • 70
0

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.

makallio85
  • 1,330
  • 2
  • 13
  • 27