简体   繁体   中英

Redirect Subdomains to Subfolders using Htaccess in Hostinger

In Hostinger, I have a limit of only 2 free subdomains. So I was looking for an alternative using htaccess. Found and tried almost every related codes on StackOverFlow, but all of them redirect to 404 page. The following is one of my attempt in 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/ . 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.

Note:

  • I am using CloudFlare for free SSL (Using http only for example in the above code).

  • 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:

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

EDIT 1:

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

EDIT 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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