简体   繁体   English

网站部分的不同域名

[英]Different domain for section of website

There are two web sites, lets call them a.com and b.com ; 有两个网站,我们称之为a.comb.com ; they've been completely separate up until now. 直到现在他们已经完全分开了。 However, there are "synergies", so the client wants to make b.com part of a.com (same media library, users, host, database et cetera)... but with it's own domain and design. 但是,存在“协同作用”,因此客户希望使b.com成为a.com的一部分(相同的媒体库,用户,主机,数据库等)......但是它拥有自己的域和设计。 A site within the site, with its own domain. 站点内的站点,具有自己的域。

SO, basically: when a user goes to a specific part of the site - let's say a.com/b, the URL should change to b.com. 所以,基本上:当用户访问网站的特定部分时 - 让我们说a.com/b,URL应该更改为b.com。

I would think I can solve this with apache virtual hosts; 我想我可以用apache虚拟主机解决这个问题; I found this aswer for how to it with Yii: Assign different domain for two controllers only 我发现这个如何使用Yii: 仅为两个控制器分配不同的域

But I'm using Wordpress (already made the main site), so I should go with vanilla PHP and Apache tweaking. 但是我正在使用Wordpress(已经成为主站点),所以我应该使用vanilla PHP和Apache调整。 Any tips and pointers are welcome. 欢迎任何提示和指示。

Please note : This is not about multisite, as that would void the benefits of a common media gallery and users etc. 请注意 :这不是关于多站点的,因为这会使公共媒体库和用户等的好处失效。

If you want to use a single WordPress installation, you either want to run WordPress Multisite (which you do not want to do it seems) with a domain mapping plugin, or then make WordPress accept any request hostname (domain) to operate on and serve content conditionally on each domain. 如果您想使用单个WordPress安装,您要么想要使用域映射插件运行WordPress Multisite(您似乎不想这样做),或者让WordPress接受任何请求主机名(域)来操作和服务有条件地在每个域上的内容。

(The following assumes you control both example.com and secondary.com and they are both pointed to the same server where the WordPress installation lives. Also the following examples have been pulled straight out of my head with no real testing or validation involved, it may or may not work as is.) (以下假设您同时控制example.comsecondary.com并且它们都指向WordPress安装所在的同一服务器。此外,以下示例已经直接从我的脑海中删除,没有涉及真正的测试或验证,它可能会也可能不会按原样运作。)

Making WP accept multiple hostnames for requests 让WP为请求接受多个主机名

Normally WordPress is tied to a single hostname/domain where it can serve pages and content. 通常,WordPress与单个主机名/域绑定,可以在其中提供页面和内容。 The domain is stored into the wp_options siteurl and home rows as http://www.example.com for example. 例如,域存储在wp_options siteurlhome行中,如http://www.example.com

You can override this in wp-config.php by using the WP_SITEURL and WP_HOME PHP constants. 您可以使用WP_SITEURLWP_HOME PHP常量在wp-config.php覆盖它。 First you need to make sure the main domain (eg maindomain.com) is set properly to the wp_options table rows siteurl and home . 首先,您需要确保将主域(例如maindomain.com)正确设置为wp_options表行siteurlhome Then you need to adjust the wp-config.php file to make WP accept any domain as such: 然后你需要调整wp-config.php文件,使WP接受任何域:

define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']);

(Make sure to adjust between HTTP and HTTPS if needed. (如果需要,请务必在HTTP和HTTPS之间进行调整。

Now your WP installation should open just fine when a domain is pointed to the server. 现在,当域指向服务器时,WP安装应该可以正常打开。 You can have as many domains as you wish and they all make WP load when accessed. 您可以拥有任意数量的域,并且在访问时它们都会加载WP。

Redirecting a page to another domain 将页面重定向到另一个域

You can use either .htaccess or WP code to do redirects. 您可以使用.htaccess或WP代码进行重定向。 If the redirect is application/site specific and should work anywhere, try to use WP code as that is more likely to follow around in source code repos and such. 如果重定向是应用程序/站点特定的并且应该在任何地方工作,请尝试使用WP代码,因为这更可能在源代码回购等中使用。

add_action('template_redirect', function () {
    $isMainDomain = strpos($_SERVER['REQUEST_URI'], 'example.com') !== false;

    // We are on primary domain and on domain.com/b
    if ($isMainDomain && is_page('b')) {
        wp_redirect('http://secondary.com');
        exit;
    }

    // You need to validate child pages too and redirect where needed.
});

Add that to a plugin or a theme's functions.php . 将其添加到插件或主题的functions.php Now when anyone is accessing the page b on the main domain, they will be redirected to secondarydomain.com . 现在,当有人访问主域上的页面b时,他们将被重定向到secondarydomain.com

To achieve this with a .htaccess you could try the following 要使用.htaccess实现此目的,您可以尝试以下方法

RewriteEngine on
RewriteBase /

// Redirect page `b` and all child pages to other domain.
RewriteCond %{HTTP_HOST} example.com$
RewriteRule ^b/(.*)$ http://www.secondary.com/$1 [L,QSA,R=301]

Removing the page slug from a page in WP 从WP中的页面中删除页面段

Now you have a user sitting at http://www.secondary.com , but the user sees the frontpage of your WP installation instead of the contents of page b . 现在您有一位用户坐在http://www.secondary.com ,但是用户看到了WP安装的首页而不是页面b的内容。

With .htaccess rewrites, you can proxy those domain requests to a certain "subdirectory" if you need to. 使用.htaccess重写,如果需要,您可以将这些域请求代理到某个“子目录”。

RewriteEngine on
RewriteBase /

# Something like the following should hide the `b` page/dir from requests.
RewriteCond %{HTTP_HOST} secondary.com$
RewriteRule ^(.*)$ http://www.secondary.com/b/$1 [L,QSA]

That rule first checks whether the request was against the secondary.com domain name. 该规则首先检查请求是否针对secondary.com域名。 If it was, the server rewrites all requests to the WordPress page b and it's subpages. 如果是,服务器会重写对WordPress页面b及其子页面的所有请求。

Now when someone opens http://www.secondary.com it should map to http://www.secondary.com/b and show the page instead. 现在当有人打开http://www.secondary.com它应该映射到http://www.secondary.com/b并显示页面。

(Note: I did not test the above rule so it may or may not work as is. (注意:我没有测试上述规则,因此可能会或可能不会按原样运行。

Fixing permalinks 修复永久链接

Now you have a problem with permalinks regarding the page b . 现在你有关于页面b固定链接有问题。 Getting the permalink returns http://www.example.com/b , when it should return http://www.secondary.com instead. 获取永久链接会返回http://www.example.com/b ,而它应该返回http://www.secondary.com

Adding a filter for permalinks should fix that issue: 为永久链接添加过滤器应解决该问题:

add_filter('post_link', function ($link, $post) {
    if (is_admin()) {
        // Don't change in admin, or editing pages may break
        return $link;
    }

    // Check here that if the $post is either page `b`
    // or a child of that page
    // then return the proper link instead.
    return 'http://www.secondary.com/foo/bar';
});

This is a shallow approach which does not consider how media or users behave. 这是一种浅薄的方法,不考虑媒体或用户的行为方式。 You need to validate that both domains allow cross-origin requests and that user session cookies are saved properly for cross-origin sessions where needed. 您需要验证两个域是否允许跨源请求,并且在需要时为正确的跨源会话正确保存用户会话cookie。

What I would also suggest is researching how to hook into the media upload process to make two WP installations use the same directory and data. 我还建议研究如何挂钩媒体上传过程,使两个WP安装使用相同的目录和数据。 The same directory is achiveable with the upload_dir filter and the attachments are saved similarly to posts so you can duplicate uploads to two sites using wp_insert_post or similar I presume. 使用upload_dir过滤器可以获得相同的目录,并且附件的保存方式类似于帖子,因此您可以使用wp_insert_post或类似的I wp_insert_post将上传内容复制到两个站点。

Additionally you can use two WP installations with a single users table using some wp-config.php trickery. 此外,您可以使用一些wp-config.php技巧将两个WP安装与单个用户表一起使用。

SO, basically: when a user goes to a specific part of the site - let's say a.com/b, the URL should change to b.com. 所以,基本上:当用户访问网站的特定部分时 - 让我们说a.com/b,URL应该更改为b.com。

Sounds like you only need to make a file/controller/post located at a.com/b which will redirect the browser to b.com . 听起来你只需要在a.com/b上创建一个文件/控制器/帖子,它会将浏览器重定向到b.com

The simplest-simplest way is to make a directory named b in the root, and put the apache default file in there (index.htm, index.php, etc), and that file would have redirection header. 最简单的方法是在根目录中创建一个名为b的目录,并将apache默认文件放在那里(index.htm,index.php等),该文件将具有重定向头。

The ideal way to do it is to put a 301 redirection using .htaccess . 理想的方法是使用.htaccess进行301重定向。 Something like 就像是

Redirect 301 /b http://b.com

or with a wildcard: 或使用通配符:

RedirectMatch 301 /b(.*) http://b.com/$1

(there are tons of htaccess resources online) (网上有大量的htaccess资源)

Those are the two extremes, and there are many other options in between, you should choose one that works for you the best based on how many redirections there are, are there any patterns, would you like to be able to generate them dynamically, etc. 这是两个极端,中间还有很多其他选项,你应该根据有多少重定向选择最适合你的选项,是否有任何模式,你想能够动态生成它们等等。


The problems you might have here (you never described the issue fully, so I'm just saying), is in case you want to return to a.com and you can't set up the similar redirection on b.com. 您可能遇到的问题(您从未完全描述过这个问题,所以我只是说),以防您想要返回a.com并且无法在b.com上设置类似的重定向。 In that case I suggest you simply say on a.com/b while having b.com rendering inside an iframe. 在这种情况下,我建议你简单地在a.com/b上说,同时在iframe中进行b.com渲染。

If you don't want to mess with wp filters or your htaccess rules 如果你不想搞乱wp过滤器或你的htaccess规则

You can simply create a index.html inside a directory called /b/ or create a page with a slug with value 'b' a.com/b with this code in plain html 你可以在一个名为/ b /的目录中创建一个index.html或者用一个带有' a.com/b /b值的slug创建一个页面,这个代码在普通的html中

<script>top.location='http://b.com'</script>

Just add a © notice to the bottom of b.com making it part of a.com (or just add "This site is part of a.com" or something like that). 只需在b.com的底部添加©通知,使其成为a.com的一部分(或者只是添加“此网站是a.com的一部分”或类似的东西)。 If you want to link a.com to b.com then add a link like 如果您想将a.com链接到b.com,请添加类似的链接
<a href="b.com">b.com</a> <a href="b.com"> b.com </a>
. In case you want a domain name inside a.com then just do a redirection from a.com/b to b.com... 如果您想在a.com中使用域名,那么只需从a.com/b重定向到b.com ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM