【问题标题】:URL Rewrite subdomain with variables to regular domain with variablesURL 将带有变量的子域重写为带有变量的常规域
【发布时间】:2015-06-29 23:37:01
【问题描述】:

我正在尝试获得有关此 URL 重写的帮助。我已经阅读了有关如何执行所有这些操作的多个教程和文档页面,但对我来说没有任何意义。我也不懂正则表达式,所以这也无济于事。我有一段半工作代码,只需要帮助让它正常工作。

我需要:http://subdomain.domain.com?dl=2

重定向到http://domain.com/subdomain.php?dl=2

我的代码是

RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !page.php
RewriteRule ^(.+/)?([^/]*)$ page.php?dl=$2 [QSA,L,NC]

发送变量但无法确定子域部分。如果有人可以帮助我,将不胜感激。

【问题讨论】:

    标签: .htaccess url url-rewriting rewrite


    【解决方案1】:

    您需要检查QUERY_STRING,因为RewriteRule 不包含它。另外,您的规则没有使用重定向标志R

    RewriteEngine on
    
    # First, check for the subdomain
    RewriteCond %{HTTP_HOST} ^subdomain.domain.com$ [NC]
    
    # Then, check the query string - it should match digits (\d+)
    RewriteCond %{QUERY_STRING} ^dl=\d+ [NC]
    
    # Check if we are not at subdomain.php
    # (This is redundant, but leaving it here in case you really need it)
    RewriteCond %{REQUEST_URI} !^/subdomain.php
    
    # If all the above conditions are true, match a root-request
    # and redirect to domain.com/subdomain.php with the query string
    # Note: You don't need to actually specify the query string
    #       in the destination URI - Apache will automatically
    #       hand it over upon redirect (using the R flag).
    #       The only time this is not the case is when you
    #       either add the QSD flag, or append the destination
    #       URI with a question mark.
    RewriteRule ^$ http://domain.com/subdomain.php [R=302,L]
    

    以上内容会将http://subdomain.domain.com/?dl=2 重定向到http://domain.com/subdomain.php?dl=2。如果您想让重定向永久化并被浏览器和搜索引擎缓存,请将302 更改为301

    【讨论】:

    • 谢谢你,正是需要的,并且有很好的解释。
    猜你喜欢
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多