【问题标题】:How to stop Wordpress from redirecting subdomain to the main domain?如何阻止 Wordpress 将子域重定向到主域?
【发布时间】:2017-08-16 13:33:27
【问题描述】:

在 stackoverflow 中搜索了此问题的答案,但找不到任何有效的答案。

我的问题是:我有一个名为 http://lasalida.net 的站点(用于马德里位置),我将它复制到一个子域以为萨拉戈萨位置设置一个站点:http://zaragoza.lasalida.net

我已经在数据库中设置了 site_url 和 home,两者都是为子域设置的。 .htaccess 中没有重定向,即使没有 htaccess 也尝试过,同样的情况。

如果我使用子域基础链接,它会重定向到主域

如果我使用子域下的任何页面,我会留在子域。

只有第一页,主页面被重定向到主域地址。

我应该怎么做才能让子域的基地址在没有重定向的情况下工作?

【问题讨论】:

  • 你更新你的永久链接了吗?
  • 是的,设置为主页面的永久链接也指向子域。
  • 请向我们展示您的 .htaccess 文件内容
  • # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress 但没关系,如果我删除 .htaccess 文件也会发生同样的情况。
  • 您应该有两个 .htaccess 文件。一个用于您的主站点(马德里位置),另一个位于您的子域站点(萨拉戈萨位置)的子目录中。

标签: wordpress .htaccess redirect subdomain


【解决方案1】:

我有相同的 URL 设置 - 我的主域上的子域 - 并且遇到了同样的问题。

我在两个目录中都有 .htaccess 文件。它们是相同的,但我根据Wordpress Codex 页面上的 .htaccess “子域示例”将子域目录中的 .htaccess 更改为下面的代码,现在子域上的开发站点工作正常:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]

【讨论】:

    【解决方案2】:

    您需要做的就是将以下内容添加到您的 wp-config.php 文件中:

    define('WP_HOME','http://subdomain.example.com');
    define('WP_SITEURL','http://subdomain.example.com');
    

    【讨论】:

      【解决方案3】:

      将包含此内容的 .htaccess 文件添加到您的子域目录库:

      RewriteEngine On
      RewriteBase /
      RewriteRule ^index\.php$ - [L]
      
      # add a trailing slash to /wp-admin
      RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
      
      RewriteCond %{REQUEST_FILENAME} -f [OR]
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule ^ - [L]
      RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
      RewriteRule ^(.*\.php)$ $1 [L]
      RewriteRule . index.php [L]
      

      【讨论】:

        【解决方案4】:

        这是我的解决方案(基于上面 skapie19s 的回答):

        // Generate home-URL based on the current request.
        $home_proto = empty($_SERVER['HTTPS']) ? 'http' : 'https';
        $home_url   = $home_proto . '://' . $_SERVER['HTTP_HOST'];
        
        // Assign the dynamic home-URL to the WordPress config.
        define( 'WP_HOME',$home_url );
        define( 'WP_SITEURL',$home_url );
        

        这个小型 sn-p 允许您为 WordPress 网站使用多个域。例如,您已将以下 URL 分配给您的网站:

        • lasalida.net
        • zaragoza.lasalida.net
        • subdomain.lasalida.net

        所有这些域都将显示同一个网站,而不会重定向到单个“主”域。

        变体:去掉“www”

        这是第二个示例,它允许同一网站使用多个(子)域,但会重定向“www”。子域到顶级域:

        // Prepare the Domain: "www.domain.com" will be changed to "domain.com".
        $home_domain = str_replace( 'www.', '', $_SERVER['HTTP_HOST'] );
        $home_proto  = empty($_SERVER['HTTPS']) ? 'http' : 'https';
        $home_url    = $home_proto . '://' . $home_domain;
        
        // Assign the dynamic home-URL to the WordPress config.
        define( 'WP_HOME',$home_url );
        define( 'WP_SITEURL',$home_url );
        

        【讨论】:

          猜你喜欢
          • 2014-01-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-11
          • 2015-06-05
          • 2012-07-22
          • 2014-10-05
          相关资源
          最近更新 更多