使用drupal重写域和子域

时间:2013-03-08 19:06:36

标签: .htaccess drupal url-rewriting

我有一个名为pure html的域名让它名为www.example.com,我有一个drupal实例作为名为dev.example.com的子域名。 example.com直接放在public_html的根目录下,而drupal实例放在public_html / dev中。我的重写如下:

# To redirect all users to access the site WITH the 'www.' prefix,
# (xhttp://example.com/... will be redirected to xhttp://www.example.com/...)
# adapt and uncomment the following:

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]

RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

# To redirect all users to access the site WITHOUT the 'www.' prefix,
# (xhttp://www.example.com/... will be redirected to xhttp://example.com/...)
# uncomment and adapt the following:

RewriteCond %{HTTP_HOST} ^www\.dev.example\.com$ [NC]

RewriteRule ^(.*)$ http://www.dev.example.com/$1 [L,R=301]

由于某种原因,我得到子域的错误,有人可以帮助我吗?

3 个答案:

答案 0 :(得分:0)

非常确定:

RewriteCond %{HTTP_HOST} ^www\.dev.example\.com$ [NC]

应该是:

RewriteCond %{HTTP_HOST} ^dev\.example\.com$ [NC]

答案 1 :(得分:0)

这可用于转发从http://www.example.com/devhttp://dev.example.com的所有内容:

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

另请注意,RewriteCond %{HTTP_HOST} ^www\.dev.example\.com$ [NC]\

之间dev缺少.example

希望有帮助...

Ĵ

:)

答案 2 :(得分:0)

以下是我对域指向和public_html目录结构的理解:

单个Drupal网站(默认)

/public_html      <-- where example.com, www.example.com, dev.example.com are pointing
    .htaccess         <-- root's .htaccess
    index.html        <-- http://example.com, http://www.example.com
    /dev                  <-- drupal root folder
        .htaccess         <-- drupal's .htaccess
        index.php         <-- http://dev.example.com
        /sites                <-- drupal's file system folder
            /default          <-- drupal's default subfolder 
            setting.php       <-- drupal's default setting (dev.example.com)

如果上述内容正确显示,我建议如下:

root&#39; s .htaccess

RewriteEngine On
RewriteCond %{HTTP_HOST} ^dev
RewriteCond %{REQUEST_URI} !^/dev/
RewriteRule ^(.*)$ dev/$1 [L]

setting.php

# $base_url = 'http://www.example.com';  // NO trailing slash!

要在您的网址上找到没有/dev/的干净网址,找到包含上述代码的行,删除主要的哈希符号并填写Drupal安装的绝对网址如下:

$base_url = 'http://dev.example.com';  // NO trailing slash!

在浏览器上打开您的网站。一旦您以管理员身份登录drupal站点,请在以下管理菜单下检查或设置公共文件系统路径的正确路径:

Home » Administration » Configuration » Media » File System

验证或更正值:

sites/dev.example.com/files, for your site dev.example.com via access url: 
http://dev.example.com/#overlay=%3Fq%3Dadmin%252Fconfig%252Fmedia%252Ffile-system  

多个Drupal网站(按子域名)

您可以考虑为您的drupal网站http://dev.example.com设置多站点配置,这样您就可以为http://prod.example.com等其他子域设置默认文件夹,如下所示:

/public_html      <-- example.com, www.example.com, dev.example.com, prod.example.com
    .htaccess         <-- root's .htaccess
    index.html        <-- http://example.com, http://www.example.com
    /dev                  <-- drupal root folder
        .htaccess         <-- drupal's .htaccess
        index.php         <-- http://dev.example.com, http://prod.example.com
        /sites                <-- drupal's file system folder
            /default              <-- drupal's default subfolder 
                setting.php       <-- drupal's default setting (prod.example.com)
            /dev.example.com      <-- drupal's dev.example.com subfolder
                setting.php       <-- drupal's dev.example.com setting

root&#39; s .htaccess

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(dev|prod)
RewriteCond %{REQUEST_URI} !^/dev/
RewriteRule ^(.*)$ dev/$1 [L]

然后在每个settings.php上删除前导哈希符号$base_url并填写Drupal安装的绝对URL。

验证或更正每个子域的公共文件系统路径的值,如下所示:

sites/dev.example.com/files, for your site dev.example.com via access url: 
http://dev.example.com/#overlay=%3Fq%3Dadmin%252Fconfig%252Fmedia%252Ffile-system  
sites/prod.example.com/files, for your site prod.example.com via access url:  
http://prod.example.com/#overlay=%3Fq%3Dadmin%252Fconfig%252Fmedia%252Ffile-system

多个Drupal网站(按子域+网址&#39; s路径)

你也可以扩展上面的多站点配置,你可以在你的网址下设置另一个drupal站点(与http://dev.example.com共享相同的drupal根文件夹) :

http://dev.example.com/site1,
http://dev.example.com/site2, etc..

其中 site1 site2 是通过以下shell命令到drupal根文件夹的符号链接

$ cd /path/to/your/public_html/dev
$ ln -s . site1
$ ln -s . site2

所以目录结构如下:

/public_html      <-- example.com, www.example.com, dev.example.com, prod.example.com
    .htaccess         <-- root's .htaccess
    index.html        <-- http://example.com, http://www.example.com
    /dev                  <-- drupal root folder
        .htaccess         <-- drupal's .htaccess
        index.php         <-- http://dev.example.com, http://prod.example.com
        /site1             <-- http://dev.example.com/site1
        /site2             <-- http://dev.example.com/site2
        /sites                <-- drupal's file system folder
            /default          <-- drupal's default subfolder 
                setting.php   <-- drupal's default setting (prod.example.com)
            /dev.example.com            <-- drupal's dev.example.com subfolder
                setting.php             <-- drupal's dev.example.com setting
            /dev.example.com.dev.site1  <-- drupal's dev.example.com.site1 subfolder
                setting.php             <-- drupal's dev.example.com.site1 setting
            /dev.example.com.dev.site2  <-- drupal's dev.example.com.site2 subfolder
                setting.php             <-- drupal's dev.example.com.site2 setting

root&#39; .htaccess 的代码与上述代码相同,步骤也填写 setting.php 中$ base_url的绝对网址,并验证或更正新站点的公共文件系统路径的值:

sites/dev.example.com.dev.site1/files, for your site dev.example.com.site1 via access url: 
http://dev.example.com/site1/#overlay=%3Fq%3Dadmin%252Fconfig%252Fmedia%252Ffile-system  
sites/dev.example.com.dev.site2/files, for your site dev.example.com.site2 via access url:  
http://dev.example.com/site2/#overlay=%3Fq%3Dadmin%252Fconfig%252Fmedia%252Ffile-system

多个Drupal网站(Drupal-8)

Drupal-8不再需要符号链接,在 / public_html / dev / sites 下为网站设置命名文件夹更加灵活,并将它们放在网站上.php 文件如下:

$sites = array(
   'dev.example.com' => 'dev',
   'dev.example.com.site1' => 'dev.site1',
   'dev.example.com.site2' => 'dev.site2',
);

所以目录结构如下:

/public_html      <-- example.com, www.example.com, dev.example.com, prod.example.com
    .htaccess         <-- root's .htaccess
    index.html        <-- http://example.com, http://www.example.com
    /dev                  <-- drupal root folder
        .htaccess         <-- drupal's .htaccess
        index.php         <-- http://dev.example.com, http://prod.example.com
        /sites                <-- drupal's file system folder
            sites.php         <-- drupal's multisite setting
            /default          <-- drupal's default subfolder 
                setting.php   <-- drupal's default setting (prod.example.com)
            /dev                  <-- drupal's dev.example.com subfolder
                setting.php       <-- drupal's dev.example.com setting
            /dev.site1            <-- drupal's dev.example.com.site1 subfolder
                setting.php       <-- drupal's dev.example.com.site1 setting
            /dev.site2            <-- drupal's dev.example.com.site2 subfolder
                setting.php       <-- drupal's dev.example.com.site2 setting

最后,为了确保一切设置正确,只要您通过访问以下菜单设置新网站,建议清除缓存

Home » Administration » Configuration » Development » Clear all Caches

为方便起见,我已为Drupal Multisite设置了一个简单的演示网站,如上所述。我还保留了这个特定主题的文档here