Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess 使用htaccess将所有虚拟子域重写到同一文件夹_.htaccess_Mod Rewrite_Url Rewriting_Subdomain - Fatal编程技术网

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

.htaccess 使用htaccess将所有虚拟子域重写到同一文件夹,.htaccess,mod-rewrite,url-rewriting,subdomain,.htaccess,Mod Rewrite,Url Rewriting,Subdomain,我一直在网上搜索,试图找到解决这个问题的办法,但找不到确切的解决办法 我正在尝试重定向重写 anything.domain.com/anypage.php?sub=anything&include=get\u参数 到 domain.com/users/anypage.php?sub=anything&include=get\u参数 我已经找到了几页可能的解决方案,但它们都与我的需求略有不同,不断编辑它们最终以失败告终 任何帮助都将不胜感激。多谢各位 启用了p.S通配符DNS,并在apache中

我一直在网上搜索,试图找到解决这个问题的办法,但找不到确切的解决办法

我正在尝试重定向重写

anything.domain.com/anypage.php?sub=anything&include=get\u参数

domain.com/users/anypage.php?sub=anything&include=get\u参数


我已经找到了几页可能的解决方案,但它们都与我的需求略有不同,不断编辑它们最终以失败告终

任何帮助都将不胜感激。多谢各位

启用了p.S通配符DNS,并在apache中正确设置了所有内容

编辑:

我最终使用了下面的方法,似乎效果很好。谢谢

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

在.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

我应该在问题中指定“anything”,我的意思是这个值将是一个变量(确切地说是用户用户名)。您不必转义。在方括号内使用文字值。但这应该行得通。并在
$1
前面添加
users/
,以匹配示例。添加HTTPS很好。@anubhava:非常感谢您在这方面的帮助。然而,我找到了一个能满足我所有需求的解决方案。我已经在原来的问题中添加了最终的工作解决方案。Thanks@Myv:谢谢你的建议,我已经按照你的建议修改了我的答案。@Myv:非常感谢你的好建议@戴夫:你建议的解决方案中没有什么问题。1)
必须在方括号外转义,否则
表示任何字符,因此条件
foo.domain.com
也将匹配
foo domain com
。2) 您可能不需要它,但您的答案中没有https处理。3) 按照您的规则创建
http://foo.domain.com/
将变为
http://domain.com/users//?sub=foo
带有双斜杠。4) 因为您没有使用QSA标志,所以重定向后原始查询字符串将丢失。