首页 文章

使用htaccess将子域重定向到主域

提问于
浏览
0

我正在 Build 一个有两个主要部分的网站,

所以我创建了两个子域:

web1.example.com web2.example.com

因为我正在使用codeigniter构建网站,所以我想重定向到index.php页面 . 这就是我做的:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^web1.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/web1 [R=301,NC,L]

它按预期工作,但它将url更改为example.com/web1 ..是否有任何方法可以保留url web1.example.com,但仍然重定向到example.com/web1?

我已经在使用.htaccess文件:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    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>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>

当我输入web1.example.com时,要执行的实际url应该是:example.com/web1,要执行的文件是index.php ..

1 回答

  • 1

    没有测试过,修改并试试这个

    RewriteRule ^([a-zA-Z0-9-/]+).example.com$ http://example.com/$1 [L]
    RewriteRule ^([a-zA-Z0-9-/]+).example.com/([a-zA-Z0-9-/]+) http://example.com/$1&$2 [L]
    

    Edited 1

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
    RewriteRule ^(.*)$ http://example.com/index.php/%1&$1 [L,NC,QSA]
    

相关问题