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_Url Rewriting - Fatal编程技术网

.htaccess 将子域重写到子目录而不重定向

.htaccess 将子域重写到子目录而不重定向,.htaccess,url-rewriting,.htaccess,Url Rewriting,我想创建虚拟子域 比如: 没有重定向 我在htaccess中使用此cdoe: RewriteEngine on DirectorySlash off RewriteBase / RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com.*$ [NC] RewriteRule ^(.*)$ http://domain.com/users/%1/$1 [P,L,NC,QSA] 但当我使用: 目录删除 我可以重写: user1.domai

我想创建虚拟子域 比如:

没有重定向

我在htaccess中使用此cdoe:

RewriteEngine on
DirectorySlash off
RewriteBase /
        RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com.*$ [NC]
        RewriteRule ^(.*)$ http://domain.com/users/%1/$1 [P,L,NC,QSA]
但当我使用: 目录删除 我可以重写: user1.domain.com/anythging/>domain.com/users/user1/anything

结尾带有斜杠(/)(anythging/), 但是这个

在第二种情况下,当我使用:

DirectorySlash ON
这:

但是我不想重写链接,也不想重定向它


知道问题出在哪里了吗?

不知道为什么
DirectorySlash
不适合你,但你并不需要它来实现你想要的。这个
.htaccess
也可以工作。我假设您希望删除任何尾部斜杠(如果存在)

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*?)/?$ http://domain.com/users/%1/$1 [L]
另外,不需要代理
[p]
、查询字符串append
[QSA]
(默认情况下是打开的)和case
[NC]
(因为重写正则表达式只包含通配符)。

我找到了解决方案:

RewriteEngine on
DirectorySlash off
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.([^\.]*)\.domain\.com$ [NC]
RewriteRule (.*) http://%1.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC] 
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*?)/?$ http://domain.com/users/%1/$1/ [P]

它可能需要一些清理,以将所有www链接重定向到非www,并将www.domain.com从子域重写中排除

而不使用[p],它将实际重定向,使用[R]它将重定向。使用[P]可以调用
mod\u proxy
。更多信息。
user1.domain.com/anythging >> redirects to >> domain.com/users/user1/anythging
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*?)/?$ http://domain.com/users/%1/$1 [L]
RewriteEngine on
DirectorySlash off
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.([^\.]*)\.domain\.com$ [NC]
RewriteRule (.*) http://%1.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC] 
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*?)/?$ http://domain.com/users/%1/$1/ [P]