【问题标题】:htaccess in subbdomain skip redirection rule子域中的 htaccess 跳过重定向规则
【发布时间】:2013-03-16 09:27:38
【问题描述】:

当使用特定的子域链接格式时,我试图让我的子域的访问者只直接进入。我知道这也会阻止 SE,但我不希望子域被索引。

允许的链接应如下所示:

subdomain.maindomain.com/aaa/bbb/ccc

应该改写成这样:

subdomain.maindomain.com/index.php?a=aaa&b=bbb&c=ccc

所有不属于这种形式并且来自空或外部引荐来源的东西都应该转到主域并且没有变量:

maindomain.com/

我尝试了各种配置,我在子域文件夹中的最后一个 .htaccess 文件如下所示:

RewriteEngine on
RewriteRule ^/?(\w{3})/(\w{3})/(\w{3})$ index.php?a=$1&b=$2&c=$3 [S=1,L]

RewriteCond %{HTTP_REFERER} !^http://(www\.)?maindomain\.com [NC]
RewriteRule ^(.*)$ http://domain\.com/ [L]

但它仍然没有做我想做的事,它还将允许表单的子域请求重定向到主域,它还将 vars 作为请求添加到主域,转到此页面

maindomain.com/?a=aaa&b=bbb&c=cee

你能帮我解决上面定义的条件吗?

关于性能的第二个问题:我显然可以使用 PHP 进行此验证/重定向,您认为哪种方式更有效?

谢谢!

【问题讨论】:

    标签: .htaccess mod-rewrite apache2


    【解决方案1】:

    如果我正确理解了你的逻辑,请试试这个:

    RewriteEngine on
    RewriteBase /
    
    # Don't do any more rewrites if on index.php
    # Note that you can add the HTTP_HOST condition here if you only want it to be active for the subdomain
    RewriteRule ^index\.php$ - [L]
    
    # If on subdomain then check that the referer is from main domain and attempt to match a regex
    # If we find a match then ignore the next rule that rewrites subdomain to domain.com
    # Basically this is like an awkward if-else statement..
    # ================    
    
    RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$
    RewriteCond %{HTTP_REFERER} ^http://(www\.)?domain\.com [NC]
    # Rewrites /aaa/bbb/ccc to /index.php?a=aaa&b=bbb&c=ccc
    RewriteRule ^(\w{3})/(\w{3})/(\w{3})$ index.php?a=$1&b=$2&c=$3 [S=1,L]
    
    # Redirect all requests from subdomain to domain.com by default
    # ================   
    
    RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$
    # Add the trailing question mark to delete all query parameters
    RewriteRule .* http://domain.com/? [L]
    

    【讨论】:

    • 谢谢,但是不行,还是不行。对subdomain.domain.com/aaa/bbb/cee 的直接访问仍会重定向到domain.com
    • 什么不起作用?你是从 subdomain.domain.com 重写到 domain.com 吗?请注意,我已经多次更新了代码。
    • @user2176640 请查看我的更新答案。我们必须在顶部添加RewriteRule ^index\.php$ - [L] 指令,因为[L] 标志并不总是能够停止所有进一步的重写处理。
    • 谢谢。使用您上次更新时,没有 subdomain.maindomain.com/aaa/bbb/ccc 格式的引用者的 URL 仍会被重定向到 maindomain.com。我开始认为可能存在其他配置错误,可能在 apaches vhosts 中?有什么想法吗?
    • @user2176640 请尝试将%{HTTP_REFERER}条件注释掉,看看是否是罪魁祸首。
    【解决方案2】:

    你已经很接近了。你只需要几个额外的部分

    • 标志S=1 没有害处,但也不是必需的。
    • 将引用条件移至第一条规则并将其反转。您可能还希望允许任何子域。
    • 添加额外的否定条件以排除 index.php 被重定向到主域。
    • 您没有在第二条规则的替换中使用原始请求,因此您不需要捕获它。
    • 附加一个问号? 以防止将参数转发到主域。
    • 添加R 标志,如果您希望客户端被重定向而不仅仅是请求被重写。

    所有部分在一起

    RewriteEngine on
    
    RewriteCond %{HTTP_REFERER} ^http://(.+\.)?maindomain\.com [NC]
    RewriteRule ^(\w{3})/(\w{3})/(\w{3})$ index.php?a=$1&b=$2&c=$3 [L]
    
    RewriteCond %{REQUEST_URI} !$index\.php$
    RewriteRule ^ http://domain\.com/? [R,L]
    

    关于性能,如果您仅使用 .htaccess 重写或重定向,则该工作甚至在遇到一些 PHP 脚本之前就已完成。所以,我认为最好使用 .htaccess 来做到这一点。

    【讨论】:

    • 谢谢。没有 subdomain.maindomain.com/aaa/bbb/ccc 格式的引用者的 URL 仍会被重定向到 maindomain.com。我开始认为可能存在其他配置错误,可能在 apaches vhosts 中?有什么想法吗?
    • @user2176640 我想,这就是你想要的。你写了“所有不是这种形式的东西,来自空的或外部的引用者,都应该去主域,没有变量”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 2012-06-11
    • 2010-12-27
    • 2010-09-26
    • 2011-11-21
    • 2012-07-31
    相关资源
    最近更新 更多