【问题标题】:redirecting file request for subdomain and main site with certain arguments?使用某些参数重定向子域和主站点的文件请求?
【发布时间】:2011-12-01 08:53:02
【问题描述】:

我正在运行通配符子域。该站点的目的是运行一个虚拟子域。功能应该是当文件 从主站点调用它们将被重写为干净的 URL。但是当我从子域中调用相同的文件时 像 xyz.doamin.com/my-file.php 那么这应该被重写,就像它使用 subdoamin 的参数调用该文件一样 domain.com/my-file.php?var=xyz。目前我为文件调用所做的是这样的

Options +FollowSymLinks
RewriteEngine on

## Redirecting all non-www domains to www.domain.com
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC,L]


RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^(.*)$   http://www.domain.com%{REQUEST_URI}?var=%1 [L]


RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING}  !^$
RewriteRule ^(.*)$   http://www.domain.com%{REQUEST_URI}?%{QUERY_STRING}&var=%1 [L]



RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^(.*)$   http://www.domain.com/index.php?subdomain=%1 [L]


RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING}  !^$
RewriteRule ^(.*)$   http://www.domain.com%{REQUEST_URI}?%{QUERY_STRING}&subdomain=%1 [L]

这会为文件调用引发 500 内部错误。

【问题讨论】:

    标签: apache .htaccess mod-rewrite url-rewriting subdomain


    【解决方案1】:

    规则集 4 将与规则集 1 和 2 重叠。我怀疑这是导致问题的原因。 所以我认为这三个都需要额外的RewriteCond 检查REQUEST_URI

    真的,您不需要规则集 4。只需包含 index.php 作为默认值就可以了。除非你当然不想要 http://www.domain.com/?var=xyz 这样的 URL,在这种情况下你需要规则 4,但你需要将它分成 2 个 - 一个带有 QUERY_STRING 和一个没有,就像规则 1 和 2 一样

    另外,%1 不是来自 rewriteRule 本身的正则表达式,而不是前面的 rewriteConds?如果是这样,那么您的正则表达式是错误的,因为它会将 var= 设置为整个原始 URL。

    查看错误日志是个好主意,因为它会告诉您 500 错误的原因。

    编辑:好的,试试这样的东西(未测试)

    ## Redirecting all non-www domains to www.domain.com
    RewriteCond %{HTTP_HOST} ^domain.com [NC]
    RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L,QSA]
    
    ## Redirecting / requests to index.php
    # Note: Use the QSA flag for handling with or without query string
    RewriteCond %{REQUEST_URI} ^/$
    RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
    RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
    RewriteRule ^(.*)$   http://www.domain.com/index.php?var=%1 [L,QSA]
    
    ## Redirecting /something requests to index.php
    # Note: Use the QSA flag for handling with or without query string
    RewriteCond %{REQUEST_URI} ^/.+$
    RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
    RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
    RewriteRule ^(.*)$   http://www.domain.com%{REQUEST_URI}?var=%1 [L,QSA]
    

    【讨论】:

    • 错误日志有这一行 :: mod_rewrite: 达到内部重定向的最大数量。假设配置错误。如有必要,使用“RewriteOptions MaxRedirects”增加限制。
    • 是的,这将是您的 RuleSet 4。它永远不会更改 HTTP_HOST,因此会不断匹配其结果。正如我所说,你最好完全放弃它。
    • @Sodved 请参阅编辑后的代码。你是这个意思吗?
    • 规则集 2 和 4 仍然重叠。规则集 3 和 5 也是如此。具有完全相同的条件。
    • 我不知道你想要达到什么目的。你希望他们在哪里结束。 http://www.domain.com%{REQUEST_URI}?var=%1http://www.domain.com/index.php?subdomain=%1
    猜你喜欢
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多