使用htaccess为每个域使用directores

时间:2016-12-28 07:59:12

标签: apache .htaccess codeigniter mod-rewrite

我有一个主机和一些域名。我想为每个域使用一个目录。不幸的是,我的主持人不支持vhosts。我在root中创建了.htaccess文件,将每个域请求重定向到它自己的目录。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

    RewriteCond %{HTTP_HOST} domain1\.com [NC]
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule ^(.*)$ /domain1/$1 [L]

    RewriteCond %{HTTP_HOST} domain2\.com [NC]
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule ^(.*)$ /domain2t/$1 [L]

</IfModule>

效果很好,我的网站可以访问&#34; http://domain1.com&#34;地址。我尝试使用CodeIgniter作为我的网站框架。 所以我将CI配置文件中的base-url配置为&#34; http://domain1.com/domain1/&#34;但它仅适用于主页面,当我请求另一个控制器,如&#34; http://doamin1.com/somecontroller&#34;时,找不到该页面。 我也在网站目录中设置了.htaccess,如下所示。

<IfModule mod_rewrite.c>

    Options +FollowSymLinks

    RewriteEngine On
    RewriteBase /domain1

    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    RewriteRule ^$ index.php [L]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
    RewriteRule ^(.*)$ index.php?/$1 [L]

</IfModule>

我该如何完成这项工作,以便浏览器地址栏不显示目录 &#34; http://doamain1.com/domain1/somecotroller&#34; (此表格正在运作)。

坦克大家。

更新 我的routes.php。

$route['default_controller'] = 'main';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

1 个答案:

答案 0 :(得分:0)

我经过一些测试后得到了我需要的答案。 我像这样更改root .htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

    RewriteCond %{REQUEST_URI} !^/domain2/
    RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.
    RewriteRule ^(.*)$ /domain2/$1 [L]


    RewriteCond %{REQUEST_URI} !^/domain1/
    RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.
    RewriteRule ^(.*)$ /domain1/$1 [L]  
</IfModule>

域目录中的每个.htaccess都如下

<IfModule mod_rewrite.c>

    Options +FollowSymLinks

    RewriteEngine On
    RewriteBase /domain1

    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php?/$1 [L]

</IfModule>

同时将config.php文件base-url更改为&#34; http://domain1.com/&#34;