I'm looking for a solution to support multiple domains on one webhosting account, which has to be done with htaccess. So the idea is, you call domainx and with htaccess the server "fakes" the webroot to a subfolder corresponding with the domain name. I allready have a "solution", but this doesn't work perfectly.
Problems I've got:
Redirects through PHP (with base_url() of CodeIginiter), result in; for example "http://www.domein1.nl/domein1.nl/".
It doesn't work on my local server.
So, the htaccess I'm currently using:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} domein1.nl$ [NC]
RewriteCond %{REQUEST_URI} !^/domein1.nl/.*$
RewriteRule ^(.*)$ /domein1.nl/$1 [L]
RewriteCond %{HTTP_HOST} domein2.nl$ [NC]
RewriteCond %{REQUEST_URI} !^/domein2.nl/.*$
RewriteRule ^(.*)$ /domein2.nl/$1 [L]
</IfModule>
The CodeIgniter PHP-code for the base_url(). The server variable "SCRIPT_NAME" adds the second domain folder, marked as problem 1. This should'nt happen if the root folder is faked correctly; but is that actually possible?
if (isset($_SERVER['HTTP_HOST']))
{
$base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
$base_url .= '://'. $_SERVER['HTTP_HOST'];
$base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
}
And last but not least, it is'nt working on my local server while I do redirect through my hosts file:
192.168.2.9 local.domein1.nl
192.168.2.9 local.domein2.nl
Sooo.. How do I fix these problems? Thanks in advance!
Edit: The problem with my local server is fixed.. cough "sudo a2enmod rewrite" did the trick..
Edit2: Since stormdrain started about folder structure, here is mine to clarify the multiple CI applications.
Main .htaccess location / webroot
/public_html/.htaccess
domain1:
/application/domain1/ (domain1 application path)
/application/system/ (shared system path)
/public_html/domain1/index.php (CI domain1 index)
domain2:
/application/domain2/ (domain2 application path)
/application/system/ (shared system path)
/public_html/domain2/index.php (CI domain2 index)
It's not totally clear what you are trying to do. Sounds like you have several domain names pointing to the same server in which you don't have full control and therefore can't set up VirtualHosts. You also want to serve the domains with a single application and not have a subfolder as part of the URL (e.g. you don't want http://www.domain1.nl/domain1.nl as the URL's home page).
If so, this might work:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} domein1.nl$ [NC]
RewriteRule ^(.*)$ /domein1/$1 [L]
RewriteCond %{HTTP_HOST} domein2.nl$ [NC]
RewriteRule ^(.*)$ /domein2/$1 [L]
</IfModule>
Then create routes in CodeIgniter to route the request:
$route["/domein2/(:any)"] = "/domein2/$1";
To get the rewrites working locally, you need to add the domain to the .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} local.domein1.nl$ [NC]
RewriteRule ^(.*)$ /domein1/$1 [L]
RewriteCond %{HTTP_HOST} local.domein2.nl$ [NC]
RewriteRule ^(.*)$ /domein2/$1 [L]
</IfModule>
Then base_url shouldn't need the SCRIPT_NAME since it is being rewritten and routed out of the URL:
if (isset($_SERVER['HTTP_HOST']))
{
$base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
$base_url .= '://'. $_SERVER['HTTP_HOST']. '/';
}
Update.
If you have a CI folder for each domain like:
/var/www/domain1/index.php
/var/www/domain1/application/
/var/www/domain1/system/
etc.
/var/www/domain2/index.php
/var/www/domain2/application/
/var/www/domain2/system/
etc.
RewriteCond %{HTTP_HOST} domain1.nl$ [NC]
RewriteRule ^(.*)$ /domain1/$1 [L]
RewriteCond %{HTTP_HOST} domain2.nl$ [NC]
RewriteRule ^(.*)$ /domain2/$1 [L]
Should do the job for you.
Well, it seems impossible to do neatly through htaccess..
O well, I "fixed" it. My fix within the CodeIgniter index.php: I replaced the declaration of the variable $application_folder with the code beneath.
define('DOMAIN', preg_replace(
"/^(www.|local.)?([^.]+).[^.]+$/i", "\\2",
$_SERVER['HTTP_HOST']
));
if ( DOMAIN == 'domain2')
$application_folder = '../application/domain2';
else
$application_folder = '../application/domain1';
I also made a minor change to the system "url_helper". Added this "static_url()" function, returning the URI to the path I save images/CSS/js etc.
if ( ! function_exists('static_url'))
{
function static_url()
{
return base_url().'static/'.DOMAIN.'/';
}
}
Only minor thing I've got to figure out is how to split up things like robots.txt
Related
I have an issue with .htaccess codeigniter on our website (www.jp.com => first project)
I just recently built a new project and i set and accessed by an aliased link. (www.jp.com/pt => second project).
A path looks like this :
first project : /var/www/jp
second project : /var/www/pt
The problem, if i access this url : "www.jp.com/pt" it's works,
but.. if i access others controller, e.g: "www.jp.com/pt/news" where i'm not set a controller as a default controller, the page shown 404 not found.
And then,i try using index.php like this, "www.jp.com/pt/index.php/news" It's works..
Can anyone help me to fixing this ? i'll can access my alias link without index.php
I using CI 2 & PHP 5.3
Here is my code (mixed from many references :D )
Current .htaccess :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|public)
RewriteRule ^/(.*)$ http://www.jp.com/$1 [NC,L,R=301]
My Config.php :
$root = "http://".$_SERVER['HTTP_HOST'];
$root.= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = "$root";
$config['index_page'] = '';
Thank you...
The .htaccess RewriteBase in the codeigniter second project directory should say /pt/
RewriteBase /pt
Otherwise the rewrite to index.php ends up going to first project router.
I am using one webserver for hosting two different websites, each having it's own domain and located in it's own subdirectory on the server. Both domains point to the root directory of my server.
This is my file structure:
root/
domain1/
domain2/
In my root directory i am using a small PHP script to determine which URL is coming in and than forward it to corresponding subdirectory.
if (($_SERVER['SERVER_NAME'] == "www.domain1.com" || $_SERVER['SERVER_NAME'] == "domain1.com") ) {
Header( "HTTP/1.1 301 Moved Permanently" );
header("location: http://www.domain1.com/domain1");
}
else if (($_SERVER['SERVER_NAME'] == "www.domain2.com" || $_SERVER['SERVER_NAME'] == "domain2.com") ) {
Header( "HTTP/1.1 301 Moved Permanently" );
header("location: http://www.domain2.com/domain2");
}
Up to here it works totally fine. When I call ww.domain1.com I get forwarded to the corresponding subdirecoty and the domain changes to www.domain1.com/domain1. This is where my question arrises: How can I hide the subdirectory in the URL? I have been struggeling with this for ages, reading guides to mod_rewrite and searching SO but didn't get anny success.
I have tested my server for RewriteEngine On, which works fine, I Just cant get the desired behaviour.
Edit:Here is my htaccess code, located in the subdirectory domain1. The same code is located in directory2, changed to the naming conventions.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain1.com$
RewriteRule !^domain1/ domain1%{REQUEST_URI} [L]
I got the idea for this from SO: SO Article
Thanks in advance ;)
you can use apache conditions in htaccess for this. create a file .htaccess with following code
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain1\.com
RewriteRule ^(.*)$ /domain1/$1 [L,NC,QSA]
RewriteCond %{HTTP_HOST} ^(.*)\.domain2\.com
RewriteRule ^(.*)$ /domain2/$1 [L,NC,QSA]
this will pass all params too, in addition to silently redirecting to sub directory. your url in browser wont show the redirection.
You can use the following code in Root/.htaccess :
RewriteEngine on
#--Rewrite domain1 to /domain1 folder--#
RewriteCond %{HTTP_HOST} ^(www\.)?domain1.com$
RewriteRule ^((?!domain1).*)$ /domain1/$1 [NC,L]
#--Rewrite domain2 to /domain2 folder--#
RewriteCond %{HTTP_HOST} ^(www\.)?domain2.com$
RewriteRule ^((?!domain2).*)$ /domain2/$1 [NC,L]
I have a Codeigniter application which was developed and tested on the normal http. Now the Apache server has been configured in such a way that all of the pages are stored in a single folder which is SSL enabled. The SSL certificate is in place. When I browse to the website using "https://www" then it redirects to "http://www".
NOTE : I do not need to load specific pages using SSL. I just need all pages to show as HTTPS.
I have tried the proposed solutions suggested on Stack Overflow including :
Modifying the .htaccess file to force HTTPS
Setting the $config['base_url'] = "https://www.yoursite.com/";
When I tried the above method in tandem then Firefox gave the following error :
The page isn't redirecting properly. Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
Many Stack Overflow contributors suggest that this should be handled by Apache only. That makes sense to me but forcing HTTPS via .htaccess gives the same error.
Is there a simple way to do this without hooks and SSL Helpers, etc?
This answer is a bit (read: a lot) late, but you can use this hook I made (5 minutes ago), It'll redirect to your HTTPS website and set some headers.
I use it along with this .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L]
It is easy,
Go to applicaton/config.php and insert the following code:
$base = "https://".$_SERVER['HTTP_HOST']; // (For https)
See line 19 on image.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$base = "https://".$_SERVER['HTTP_HOST']; // HERE THE CORRECT CODE
$base .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $base;
Step 1
Setting the $config['base_url'] = "https://www.yoursite.com/";
Step 2
Use and edit this .htaccess file.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Step 1: application/hooks/ssl.php
function force_ssl()
{
$CI =& get_instance();
$CI->config->config['base_url'] = str_replace('http://', 'https://', $CI->config->config['base_url']);
if ($_SERVER['SERVER_PORT'] != 443) redirect($CI->uri->uri_string());
}
Step 2: application/configs/hooks.php
$hook['post_controller_constructor'][] = array(
'function' => 'force_ssl',
'filename' => 'ssl.php',
'filepath' => 'hooks'
);
Step 3:application/config/config.php
$config['enable_hooks'] = TRUE;
htaccess Changes :
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
'mod_rewrite' module to be installed on your Apache server
Final: restart Apache server.
CI_app\application\config\config.php
$base_url = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$base_url .= "://". #$_SERVER['HTTP_HOST'];
$base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $base_url;
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
# Require HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# Removing the index.php file - https://codeigniter.com/userguide3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I have my index.php inside a subfolder which is inside another subfolder, etc. So, my hosting company doesn't let me modify the httpd-vhosts.config so the only way I have to indicate that I want to run my web from /subfolder1/subfolder2.... is the .htaccess.
I've searched a lot in stackoverflow, and all the stuff didn't work. Basically, I have these folders :
web
instances
myweb
public
root
Inside root, there is the index.php. So , when I enter at: www.mydomain.com.mialias.net, I want to go to index.php and run my website,
I've tried this :
RewriteEngine on
RewriteCond % ^midomain.com.mialias.net\.com$
RewriteRule (.*) http://www.midomain.com.mialias.net/$1 [R=301,L]
RewriteRule ^$ instances/myweb/public/root [L]
And it didn't work because the URL is like this : www.midomain.com.mialias.net/instances/myweb/public/root , which is wrong because I need to have my URL like : www.midomain.com.mialias.net/ in order to insert arguments on the URL, like : www.midomain.com.mialias.net/myaccount, and at the moment this is impossible because I have the URL as I said.
Does anyone know how to do it? Thank you very much!
Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com\.mialias.net\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/instances/myweb/public/root
RewriteRule ^(.*)$ /instances/myweb/public/root/ [L]
I have some domains:
http://domainmain.com
http://domainone.com
http://domaintwo.com
My secondary domains are currently hosted under the main domain. No sub-directories, no other paths. So every domain get the contents of http://domainmain.com.
For better understanding: These files points all to the same file: http://domainmain.com/index.php, http://domainone.com/index.php, http://domaintwo.com/index.php.
For every domain I have a folder located at http://domainmain.com:
domainname folder / path
-------------- -----------
domainmain.com /
domainone.com /domainone
domaintwo.com /domaintwo
My goal is to redirect every domain to the corresponding dir / path http://domainone.com.
For example: http://domainone.com has to show the content of path /domainone. The visiter has to see http://domainone.com. This also should work: http://domaintwo.com/images shows http://domainmain.com/images.
Some code I started with in the .htaccess file:
RewriteCond %{HTTP_HOST} domainone.com [NC]
RewriteCond %{REQUEST_URI} !^/domainone
RewriteRule ^(.*)$ /domainone/$1 [NC,L]
And some PHP (but I want to use redirect instead of file_get_contents():
if ($_SERVER['SERVER_NAME'] == 'domaintwo.com') {
echo file_get_contents('http://domainmain.com/domaintwo');
die();
}
Note: It is only possible to have an .htaccess file at http://domainmain.com. My server runs PHP5.
Your question is quite similar to this one asked on the web-master's section;
How to redirect different domains to separate subdirectories.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(domainone)\.com$ [NC]
RewriteRule (?!^domainone(/.*|)$)^.*$ /%1%{REQUEST_URI} [NC,L]
RewriteCond %{HTTP_HOST} ^(domaintwo)\.com$ [NC]
RewriteRule (?!^domaintwo(/.*|)$)^.*$ /%1%{REQUEST_URI} [NC,L]