URL 将带变量的子域重写为带变量的常规域

URL Rewrite subdomain with variables to regular domain with variables

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

我需要: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= [QSA,L,NC]

发送变量但无法找出子域部分。如果有人能帮助我,将不胜感激。

您需要检查 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 to http://domain.com/subdomain.php?dl=2。如果您想使重定向永久化并由浏览器和搜索引擎缓存,请将 302 更改为 301