Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.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_Subdomain - Fatal编程技术网

.htaccess如果存在,将子域重写到文件夹

.htaccess如果存在,将子域重写到文件夹,.htaccess,url-rewriting,subdomain,.htaccess,Url Rewriting,Subdomain,我正在尝试将此url customer1.example.com重写为。 有了这段代码,一切正常 RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [NC] RewriteCond %{HTTP_HOST} !www.example.com$ [NC] RewriteRule ^(.*)$ http://example.com/%1/$1 [L,R=301] 但是如果根目录中不存在文件夹customer2.example.com,我想将URL重写为ex

我正在尝试将此url customer1.example.com重写为。 有了这段代码,一切正常

RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/%1/$1 [L,R=301]
但是如果根目录中不存在文件夹customer2.example.com,我想将URL重写为example.com的根目录

条件1 customer1.example.com=>customer1文件夹存在,因此重写为example.com/customer1。 条件2 customer2.example.com=>customer2文件夹不存在,因此请重写到example.com

另一个我认为需要的是重写只到达example.com,而不是我服务器上的其他域。

感谢您的帮助

只需添加以下代码:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.html [L]
这将重定向到任何未找到页面上的index.html

对于子域重定向,请尝试以下操作:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteRule ^(.*)$ /subdomains/sub/$1 [L,NC,QSA]

您好,经过长时间的搜索和测试,我找到了解决方案

现在检查所有子域请求。htaccess检查子域是否存在,如果不存在,请尝试重写到根域中的文件夹,如果不存在,请重写到根域

RewriteEngine On

#=============================
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/example.com/%1/ -d
RewriteRule ^(.*)$ https://example.com/%1/$1 [P,R=301]
#=============================
现在,我希望重定向正常进行,但地址栏中的url不会更改。像这样:

https://example.com/foo => https://example.com
我的网站在这个foo目录中


谢谢您的帮助

谢谢,但这不起作用,我只需要将subdomain.example.com重定向到example.com/subdomain,或者如果子域目录不存在,请重写到example.com谢谢,这可能与我的相同。我的问题是如何重写到根http//如果子文件夹不存在,则为domain.com。“但是如果文件夹customer2.example.com不存在”-这是错误的措辞,
sub.example.com
也不作为文件夹存在。您要检查的是,文档根目录中是否存在实际的物理文件夹
customer2
(另一种情况下为
sub
)。因此,您需要首先通过重写来提取主机名的子域部分,然后检查是否与DR下的现有文件夹匹配,第二个文件夹使用第一个文件夹中捕获的部分的反向引用。是的,这正是我需要的,我将试着更好地解释。