【问题标题】:htaccess disallow redirect specific subdomainhtaccess 不允许重定向特定的子域
【发布时间】:2021-04-04 05:28:51
【问题描述】:

我重定向所有虚拟子域如下。

RewriteCond %{HTTP_HOST} ^(.+)\.mysite\.com$
RewriteRule ^$  subdomain.php?url=%1  [NC,QSA,L]

这样,我从数据库中获取数据。所有“虚拟子域”。但是有一个真正的子域。 Opencart 里面 (shop.mysite.com)

htaccess 也指导这一点。如何防止它仅仅重定向“shop”这个词。

我的完整代码:

Options Indexes SymLinksIfOwnerMatch
RewriteEngine on
directoryindex index.php
Options -indexes
RewriteRule index index.php
RewriteCond %{HTTP_HOST} ^(.+)\.mysite\.com$
RewriteRule ^$  subdomain.php?url=%1  [NC,QSA,L]

# www.site.com  >>  site.com
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

【问题讨论】:

  • 旁白: 你的规则只匹配文档根目录;对吗?

标签: .htaccess subdomain virtual


【解决方案1】:
RewriteCond %{HTTP_HOST} ^(.+)\.mysite\.com$
RewriteRule ^$  subdomain.php?url=%1  [NC,QSA,L]

您可以添加另一个条件来排除shop 子域。例如:

RewriteCond %{HTTP_HOST} !^shop\.
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteRule ^$ subdomain.php?url=%1  [QSA,L]

!前缀否定了正则表达式,所以条件不匹配时成功。

或者,您可以在原始条件正则表达式中使用负前瞻。例如:

RewriteCond %{HTTP_HOST} ^((?!shop\.).+)\.example\.com$
RewriteRule ^$ subdomain.php?url=%1  [QSA,L]

但是,您的指令也有错误的顺序。删除www 子域的外部重定向应首先出现,否则您需要将另一个例外合并到上述规则中。

例如:

Options SymLinksIfOwnerMatch
DirectoryIndex index.php

RewriteEngine on

RewriteRule index index.php

# www.site.com  >>  site.com
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteCond %{HTTP_HOST} !^shop\.
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteRule ^$ subdomain.php?url=%1  [QSA,L]

您还添加了 Indexes 选项,只是为了使用稍后的指令再次删除它。

【讨论】:

    猜你喜欢
    • 2019-06-29
    • 1970-01-01
    • 2023-03-27
    • 2019-10-13
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多