使用某些参数重定向子域和主站点的文件请求?
我正在运行通配符子域。该网站的目的是运行一个虚拟子域。功能应该是当文件 从主站点调用它们将被重写为干净的 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 内部错误。
I am running wildcard subdomains. the aim of the site is to run a virtual subdomain. what the functionality should be is that when the files
from the main site are called they will be rewritten for the clean URLs. But when i am calling the same files form the subdomain
like xyz.doamin.com/my-file.php then this should be rewritten like it is calling that file with an argument of subdoamin like
domain.com/my-file.php?var=xyz. Currently what i have done for the file call is this
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]
This is throwing a 500 internal error for the file call.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
规则集 4 将与规则集 1 和 2 重叠。我怀疑这是导致问题的原因。
所以我认为这三个都需要对
REQUEST_URI
进行额外的RewriteCond
检查。实际上,您不需要规则集 4。只需将 index.php 作为默认值就可以解决问题。当然,除非您不想要像
http://www.domain.com/?var=xyz
这样的 URL,在这种情况下您需要规则 4,但您需要将其拆分为 2 - 1有一个QUERY_STRING
和一个没有,就像规则 1 和 2 一样。另外,
%1
不是来自 rewriteRule 本身的正则表达式,而不是前面的 rewriteConds 吗?如果是这样,那么您的正则表达式是错误的,因为它将把var=
设置为整个原始 URL。查看错误日志是个好主意,因为它会告诉您 500 错误的原因。
编辑:好的,尝试这样的事情(未经测试)
Rule set 4 will overlap with rule set 1 and 2. I suspect this is causing the issue.
So I think all 3 need an extra
RewriteCond
check onREQUEST_URI
.Really you shouldn't need rule set 4. Just including index.php as a default should do the trick. Unless of course you don't want URL's like
http://www.domain.com/?var=xyz
, in this case you need rule 4, but you need to split it into 2 - one with aQUERY_STRING
and one without, just like rule 1 and 2Also, isn't
%1
from the regex in the rewriteRule itself and not the preceeding rewriteConds? If so then your regex is wrong, because it'll be setting thevar=
to the entire original URL.Looking in the error log is a good idea as it will give you the reason for the 500 error.
Edit: Ok, try something like this (not tested)