0

当访问者对我的子域执行请求时,我想将访问者重定向到我的主域,然后是不匹配的 URI。

例如,访问者可以访问资源,sub.domain.com/product/10但当domain.com他尝试访问与product/:id我的子域不匹配的其他资源(如sub.domain.com/anOtherResource.

我必须用 apache 重写模块来做到这一点。我发现!操作员可以完成这项工作,但它不适合我。

这是我的 .htaccess 文件中的重写配置:

RewriteCond %{HTTP_HOST} sub\.domain\.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteRule ^(.*)$ http://www.domain.com [L,R]

我还测试了这个配置:

RewriteCond %{HTTP_HOST} sub\.domain\.com
RewriteRule !^/product/[0-9]+$ http://www.domain.com [L,R]

不知道哪里错了。。。

[编辑]

.htaccess 文件是为 Wordpress 配置的。这是整个 .htaccess :

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTP_HOST} sub.domain.com
    RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
    RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R]

    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>
4

2 回答 2

4

问题出在 Wordpress 重写配置中,发生了什么?

步骤1

我在 sub.domain.com/product/1 执行请求,因此它与这些 RewriteCond不匹配:

RewriteCond %{HTTP_HOST} sub.domain.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$

然后,它不会重定向到www.domain.com

第2步

它继续重写到下一个 Cond(Worpdress 重写):

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

在这一步,它发送一个内部重定向(不发送给客户端)到sub.domain.com/index.php

第 3 步

由于重定向,它再次应用之前的 RewriteCond

RewriteCond %{HTTP_HOST} sub.domain.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R]

Cond 匹配,然后将客户端重定向到www.domain.com我没想到的位置。

解决方案

我通过在 index.php 上添加 RewriteCond 来解决问题,如下所示:

RewriteCond %{HTTP_HOST} sub.domain.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R]

现在它管理由 Worpress 重写发送的内部重定向。

于 2012-06-21T16:00:45.680 回答
0

重写规则看起来不错,但请尝试以下操作:

RewriteCond %{HTTP_HOST} ^sub[.]domain[.]com$
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteRule ^(.*)$ http://www.domain.com [L,R]

如果这不起作用,那么您可能需要复制并粘贴 VirtualHost 配置的全部内容,因为它可能是导致问题的其他原因。

于 2012-06-12T12:18:32.900 回答