我已经在XAMPP中添加了域和子域,vps服务器由Windows Server 2022运行。当我输入sub.domian.com时,它会进入htdocs中的文件夹。但是当我输入domain.com/sub.domian.com时,它也会进入htdocs中的sub.domian.com文件夹,但我不想要它。我只想从 sub.domain.com 网址访问 sub.domain.com 文件夹,而不是任何其他网址。我已经通过 Let's Encrypt 为domain.com 创建了通配符 SSL 证书。我的配置如下:
在 C:\Windows\System32\drivers tc\hosts
127.0.0.1 domain.com
127.0.0.1 sub.domain.com
IN httpd-vhosts.conf 用于域添加
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot "C:/XAMPP/htdocs"
ServerName domain.com
ServerAlias www.domain.com
ErrorLog "logs/domain.com-error.log"
CustomLog "logs/domain.com-access.log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot "C:/XAMPP/htdocs/sub.domain.com"
ServerName sub.domain.com
ServerAlias www.sub.domain.com
ErrorLog "logs/sub.domain.com-error.log"
CustomLog "logs/sub.domain.com-access.log" common
</VirtualHost>
在 httpd-ssl.conf 添加 SSL 证书
<VirtualHost *:443>
ServerAdmin webmaster@localhost
DocumentRoot "C:/XAMPP/htdocs"
ServerName domain.com
ServerAlias www.domain.com
SSLEngine on
SSLCertificateFile "C:/SSL_Certificates/domain_com_cert.pem"
SSLCertificateKeyFile "C:/SSL_Certificates/domain_com_privkey.pem"
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster@localhost
DocumentRoot "C:/XAMPP/htdocs/sub.domain.com"
ServerName sub.domain.com
ServerAlias www.sub.domain.com
SSLEngine on
SSLCertificateFile "C:/SSL_Certificates/domain_com_cert.pem"
SSLCertificateKeyFile "C:/SSL_Certificates/domain_com_privkey.pem"
</VirtualHost>
如何阻止其他网址访问 sub.domain.com 文件夹?
我通过在 XAMPP 的 htdocs 文件夹中添加 .htaccess 文件解决了这个问题。这是代码:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/addonfolder/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
现在domain.com/sub.domain.com无法访问htdocs中的sub.domain.com文件夹,它返回404错误。