-1

Hi I would like to point few domains to one of my server which contains few simple wordpress sample sites. I want to show website according to domain name. Is there any way that I can change the document root using PHP? I managed to use the below code but the problem is all domains goes to only wp_domain1 folder.

If possible, I want to use PHP for this so I can add more domains easily.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain1com$
RewriteCond %{REQUEST_URI} !wp_domain1/
RewriteRule (.*) /wp_domain1/$1 [L]
RewriteCond %{HTTP_HOST} ^domain2.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain2.com$
RewriteCond %{REQUEST_URI} !wp_domain2/
RewriteRule (.*) /wp_domain2/$1 [L]
010 Pixel
  • 195
  • 3
  • 14
  • 1
    if you have access to apache configuration, look at apache virtual host. http://httpd.apache.org/docs/2.4/vhosts/ – Jigar Aug 05 '15 at 07:26
  • I want to use .htaccess and if possible, I want to use PHP for this so I can add more domains easily. – 010 Pixel Aug 05 '15 at 07:28
  • Do you access your site like this `http://www.domain1.com/wp_domain1/index.php` & `http://www.domain2.com/wp_domain2/index.php` ? – Jigar Aug 05 '15 at 07:31
  • Yes I can access the site like that. I am searching for a way to remove those sub folders. – 010 Pixel Aug 05 '15 at 07:33
  • I can only think of vhosts(which will solve sub directory issue too, coz they are the documentroot which vhost conf will get applied to). Now I want to know if this is possible through .htaccess :) – Jigar Aug 05 '15 at 07:35
  • I managed to access index.php file under each folder with some script in .htaccess (which I have deleted :'( now) but while accessing sub-folder there was issue. – 010 Pixel Aug 05 '15 at 07:37

1 Answers1

0

First of all i would like to suggest just following Jigar's option by using vhosts and creating vhosts manually or automatically recognize the folders and listening to those for a domain.

If you don't want to do that and you are only installing wordpress. Look at Wordpress build in ways to install multiple sites/domains. https://codex.wordpress.org/Create_A_Network

When you still want to create php code to write the .htaccess file for you, you can use this.

<?php
$domains = array("domain1.com" => "wp_domain1", "domain2.com" => "wp_domain2");

$fileName = "/path/to/your/.htaccess";

touch($fileName);
$file = fopen($fileName, 'w');

fwrite($file, "RewriteEngine On");
foreach($domains as $domain => $folder) {

    fwrite($file, "
RewriteCond %{HTTP_HOST} ^". $domain ."$ [NC]
RewriteCond %{REQUEST_URI} !". $folder ."/ [NC]
RewriteRule ^(.*)$ /". $folder ."/$1 [L]");
}

fclose($file);
?>

This creates a .htaccess file like the following. Which works for me to map subdomains to a folder without the folder showing up in the URL.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain1.com$
RewriteCond %{REQUEST_URI} !wp_domain1/
RewriteRule (.*) /wp_domain1/$1 [L]
RewriteCond %{HTTP_HOST} ^domain2.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain2.com$
RewriteCond %{REQUEST_URI} !wp_domain2/
RewriteRule (.*) /wp_domain2/$1 [L]
Daan
  • 490
  • 5
  • 15
  • This is great solution. But it creates the same .htaccess as what I had written manually. This is useful when I create .htaccess only for the domain being accessed. But in case of many people accessing the page from different domains, it will result into error. – 010 Pixel Aug 18 '15 at 06:14
  • @010Pixel The .htaccess you have written will work for multiple users on different domains. I have an pretty identical one running on my servers. – Daan Aug 18 '15 at 09:35
  • Here are the live examples: http://microtradenet.info/ and http://justabbr.com. Yes when the page loads they work well but you see the links are not going to correct location. But if i add only one domain in .htaccess then they work very well. – 010 Pixel Aug 19 '15 at 07:27
  • The links are not going to the right location because all the links of microtradenet.info point to justabbr.com. You haven't changed the links in the wordpress database and/or config of microtrade.net – Daan Aug 19 '15 at 07:52