使用 htaccess 将所有虚拟子域重写到同一文件夹

发布于 2024-11-01 08:28:52 字数 672 浏览 1 评论 0原文

我一直在互联网上搜索试图找到解决这个问题的方法,但找不到明确的解决方案。

我正在尝试 redirect 重写

anything.domain.com/anypage.php?sub=anything&include=get_parameters

domain.com/users/anypage.php?sub=anything&include=get_parameters


我找到了几个包含可能解决方案的页面,但它们与我的需求略有不同,并且不断编辑它们以失败告终。

任何帮助将不胜感激。谢谢。

PS 通配符 DNS 已启用,并且 apache 中的所有设置均正确。

编辑:

我最终使用了以下内容,它似乎工作得很好。谢谢。

RewriteCond %{HTTP_HOST}
^(.*).domain.com RewriteCond
%{HTTP_HOST} !^www.domain.com [NC]
RewriteRule ^(.*)$
http://domain.com/users/$1?sub=%1 [P]

I have been searching the internet trying to find a solution to this problem but cannot find a definite solution.

I am trying to redirect rewrite

anything.domain.com/anypage.php?sub=anything&including=get_parameters

to

domain.com/users/anypage.php?sub=anything&including=get_parameters


I have found several pages with possible solutions but they all differ slightly to my needs, and editing them continually ends in failure.

Any help would be much appreciated. Thank you.

P.S Wildcard DNS are enabled and everything is set up correctly in apache.

Edit:

I ended up using the folowing, it seems to work pretty well. Thanks.

RewriteCond %{HTTP_HOST}
^(.*).domain.com RewriteCond
%{HTTP_HOST} !^www.domain.com [NC]
RewriteRule ^(.*)$
http://domain.com/users/$1?sub=%1 [P]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

£冰雨忧蓝° 2024-11-08 08:28:52

在您的 .htaccess 文件中尝试此规则:

Options +FollowSymLinks
RewriteEngine on

# redirect for http
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^(.*)$ http://domain.com/users/$1?sub=%1 [R=301,QSA,L,NE]

# redirect for https
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^(.*)$ https://domain.com/users/$1?sub=%1 [R=301,QSA,L,NE]

R=301 will redirect with https status 301
L will make last rule
NE is for no escaping query string
QSA will append your existing query parameters

$1 is your REQUEST_URI

Try this rule in your .htaccess file:

Options +FollowSymLinks
RewriteEngine on

# redirect for http
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^(.*)$ http://domain.com/users/$1?sub=%1 [R=301,QSA,L,NE]

# redirect for https
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^(.*)$ https://domain.com/users/$1?sub=%1 [R=301,QSA,L,NE]

R=301 will redirect with https status 301
L will make last rule
NE is for no escaping query string
QSA will append your existing query parameters

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