使用 WordPress 进行通配符域名重定向

发布于 2024-11-02 04:06:51 字数 1429 浏览 1 评论 0原文

我有两个带有子域的域名设置如下...

blog.domain.com
www.blog.domain.com

blog.domain.info
www.blog.domain.info

两个域都指向同一服务器上的同一位置,即包含 WordPress 的目录。 (domain.com/blog)

为了让 Google 满意,我希望所有内容都重定向到这个域...

blog.domain.com

这是 WordPress 目录中包含的 .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

所以我进入我的 cPanel 并添加了一个 301域名重定向如下...

blog.domain.info -> blog.domain.com(“www”可选并选择通配符)。

然后,cPanel 根据 WordPress 重写规则自动将以下内容添加到同一个 .htaccess 文件中...

RewriteCond %{HTTP_HOST} ^blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.info$
RewriteRule ^(.*)$ "http\:\/\/blog\.domain\.com$1" [R=301,L]

问题是通配符部分似乎不起作用。

当我访问 blog.domain.info 时,我会按预期重定向到 blog.domain.com

但是当我转到 blog.domain.info/my-post 时,我根本没有被重定向。

  1. 我该如何解决这个问题?我尝试过重写我知道有效的规则,但我能想到的只是 WordPress 规则正在干扰。

  2. 修复后,我可以将这些 mod 重写移至托管帐户 www 根目录中的主 .htaccess 中,使它们与 WordPress 规则分开吗? 编辑:回答 - 不,它们不会工作,因为它们是停放在根 www 之外的目录中的域。

谢谢!

I have two domain names setup with sub-domains as follows...

blog.domain.com
www.blog.domain.com

blog.domain.info
www.blog.domain.info

Both domains are pointing to the same location on the same server, a directory containing WordPress. (domain.com/blog)

To keep Google happy, I want everything to redirect to this one domain...

blog.domain.com

Here is what's inside the .htaccess file contained in the WordPress directory...

# 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

So I went into my cPanel and added a 301 Domain Redirect as follows...

blog.domain.info -> blog.domain.com (with "www" optional and wildcard selected).

cPanel then automatically added the following to the same .htaccess file under the WordPress rewrite rules...

RewriteCond %{HTTP_HOST} ^blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.info$
RewriteRule ^(.*)$ "http\:\/\/blog\.domain\.com$1" [R=301,L]

The problem is that the wildcard portion does not seem to work.

When I go to blog.domain.info, I get redirected to blog.domain.com as expected.

But when I go to blog.domain.info/my-post, I do not get redirected at all.

  1. How can I fix this? I've tried rewrite rules that I know work but all I can think of is that the WordPress rules are interfering.

  2. Once it's fixed, can I move these mod-rewrites to the main .htaccess in the hosting account's www root keeping them separate from the WordPress rules? Edit: Answer- NO, they will not work because they are domains parked in directories off the root www.

Thank-you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静赏你的温柔 2024-11-09 04:06:51

看来我只需将新规则移至 WordPress 部分上方即可。我还添加了一个新的域名,该域名应该从 .com 域名中删除“www”。

这一切似乎都在发挥作用。

任何意见表示赞赏。

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.com$
RewriteRule ^(.*)$ "http\:\/\/blog\.domain\.com\/$1" [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

编辑

应该注意的是,如果您更改这些 WordPress 注释之间包含的 htaccess 文件中的任何内容,它可能会在稍后被 Wordpress 覆盖。将您的自定义编辑移至此块之外和上方也是可行的,并且不受 Wordpress 本身对 htaccess 所做的任何更改的影响。

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.com$
RewriteRule ^(.*)$ "http\:\/\/blog\.domain\.com\/$1" [R=301,L]

# 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

Looks like I simply had to move the new rules above the WordPress section. I also added a new one that is supposed to remove the 'www' from the dot com domain name.

This all seems to be working.

Any comments appreciated.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.com$
RewriteRule ^(.*)$ "http\:\/\/blog\.domain\.com\/$1" [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

EDIT

It should be noted that if you change anything in the htaccess file contained between these Wordpress comments, it might get overwritten by Wordpress at a later time. Moving your custom edits outside AND above this block also works and is immune from any changes to htaccess made by Wordpress itself.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.com$
RewriteRule ^(.*)$ "http\:\/\/blog\.domain\.com\/$1" [R=301,L]

# 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
总以为 2024-11-09 04:06:51

我的网站 MethodShop 拥有多个域,包括 methodshop.com、methodshop.net 等。在进行维护时,我会在不同网站之间转移流量,这样我的用户的体验就不会中断。下面是我使用的 htaccess 通配符脚本。它采用用户尝试访问的任何 URL 并镜像另一个域上的链接。

例如,
http://methodshop.NET/games/play/bubbblewrap/index.shtml

将重定向到
http://methodshop.COM/games/play/bubblewrap/index.shtml

这是 methodshop.net 的 htaccess 脚本,它将所有 methodshop.net URL 重写为 methodshop.com。只需针对您的域进行编辑即可。

RewriteEngine 上

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

i own multiple domains for my site MethodShop, including methodshop.com, methodshop.net, etc.. When doing maintenance, i'll divert traffic between the different sites so my users don't have their experience interrupted. below is the htaccess wildcard script i use. it takes whatever URL the user attempts to access and mirrors that link on another domain.

for example,
http://methodshop.NET/games/play/bubblewrap/index.shtml

would redirect to
http://methodshop.COM/games/play/bubblewrap/index.shtml

here's the htaccess script for methodshop.net that rewrites all methodshop.net URLs to methodshop.com. just edit it for your domain.

RewriteEngine on

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文