简体   繁体   English

在 Hostinger 中使用 Htaccess 将子域重定向到子文件夹

[英]Redirect Subdomains to Subfolders using Htaccess in Hostinger

In Hostinger, I have a limit of only 2 free subdomains.在 Hostinger 中,我只有 2 个免费子域的限制。 So I was looking for an alternative using htaccess.所以我正在寻找使用 htaccess 的替代方案。 Found and tried almost every related codes on StackOverFlow, but all of them redirect to 404 page.在 StackOverFlow 上找到并尝试了几乎所有相关代码,但都重定向到 404 页面。 The following is one of my attempt in htaccess:以下是我在 htaccess 中的尝试之一:

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

If you observe, I am trying to redirect http://subdomain.domain.com to http://www.domain.com/subdomain/ .如果您观察到,我正在尝试将http://subdomain.domain.com重定向到http://www.domain.com/subdomain/ I think its mostly my host, which is not allowing me to override their limit of 2 subdomains, but there has to be a way around.我认为它主要是我的主机,这不允许我覆盖他们的 2 个子域的限制,但必须有办法解决。

Note:笔记:

  • I am using CloudFlare for free SSL (Using http only for example in the above code).我正在使用 CloudFlare 来获得免费的 SSL(在上面的代码中仅使用 http 为例)。

  • I don't want to change my host, because all my requirements are satisfied except this issue.我不想改变我的主机,因为除了这个问题,我的所有要求都得到了满足。

Try this, it should redirect the sub domain to a sub folder:试试这个,它应该将子域重定向到子文件夹:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$
RewriteCond %{REQUEST_URI} !^/subdomain/
RewriteRule (.*) /subdomain/$1

I am a little concerned in how it will work with your 2 subdomain limit, below is another alternative for you to try also:我有点担心它将如何处理您的 2 个子域限制,以下是您也可以尝试的另一种选择:

RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$
RewriteRule ^$ http://www.domain.com/subdomain/?&%{QUERY_STRING}

EDIT 1:编辑 1:

RewriteCond %{HTTP_HOST} ^([^/.]+)\.domain\.com$ 
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule (.+)$ "http://domain.com/%1" [L,P]

EDIT 2:编辑2:

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subdomain(/|$) [NC]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*)$ subdomain/$1 [L]

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

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