使用某些参数重定向子域和主站点的文件请求?

发布于 2024-12-08 06:32:40 字数 1302 浏览 1 评论 0原文

我正在运行通配符子域。该网站的目的是运行一个虚拟子域。功能应该是当文件 从主站点调用它们将被重写为干净的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浴红衣 2024-12-15 06:32:40

规则集 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 错误的原因。

编辑:好的,尝试这样的事情(未经测试)

## 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]

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 on REQUEST_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 a QUERY_STRING and one without, just like rule 1 and 2

Also, 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 the var= 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)

## 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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文