简体   繁体   English

通过.htaccess实时运行子域+从URL隐藏子目录

[英]Subdomain on the fly via .htaccess + hide subdirectory from URL

Currently I'm trying to have an environment that allows me to create a subdomain by creating subdirectory on the fly. 当前,我正在尝试一个允许我通过动态创建子目录来创建子域的环境。

Directory tree, 目录树,

domain.com
  ├── 1
  |   └── product1
  |   └── index.html
  ├── 2
  │   └── product2
  └── 3
      └── product3

.htaccess rule, .htaccess规则,

#Redirect subdomain to subdirectory
RewriteCond %{HTTP_HOST} ^([^\.]+).domain.com$
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteRule ^ %{DOCUMENT_ROOT}/%1%{REQUEST_URI} [QSA

Now, I have managed to use the .htaccess rule above to redirect me when I type in "1.domain.com" to reach index.html under subdirectory 1. 现在,当我键入“ 1.domain.com”以到达子目录1下的index.html时,我已经设法使用上述.htaccess规则来重定向我。

But if I go to "1.domain.com/product1" I will reach to my product1 page but the URL will show as "1.domain.com/1/product1". 但是,如果我转到“ 1.domain.com/product1”,则将转到我的product1页面,但URL将显示为“ 1.domain.com/1/product1”。

How can I hide the "1" between the domain.com and product1 such that it will still show as "1.domain.com/product1" but in the background it will access "1.domain.com/1/product1" directory? 如何隐藏domain.com和product1之间的“ 1”,使其仍显示为“ 1.domain.com/product1”,但在后台它将访问“ 1.domain.com/1/product1”目录?

I have done a lot of research and found that usually people will ask to hard code the name of the directory but it won't work in this case because people will create subdirectories on the fly to get different subdomains so I will need a generic .htacess rule to handle this. 我已经做了很多研究,发现通常人们会要求对目录名称进行硬编码,但是在这种情况下它是行不通的,因为人们会动态创建子目录以获取不同的子域,因此我需要一个通用的。 htacess规则来处理此问题。

How can I achieve this? 我该如何实现?

Appreciate your help! 感谢您的帮助!

To rewrite 改写

  • /product1 /产品1

to

  • /1/product1 / 1 /产品1

You can use the following rule : 您可以使用以下规则:

RewriteRule ^((?!1).+)$ /1%{REQUEST_URI} [NC,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM