user1812076
user1812076

Reputation: 279

Redirect all folders to subdomains htaccess

I am trying to redirect every folder from domain.com to it's subdomain.

Examples:

domain.com/a -> a.domain.com
domain.com/b -> b.domain.com
domain.com/about -> about.domain.com

So basically, every folder redirected to it's subdomain. For the first example, folder a contains index.html. When I browser to domain.com/a it should redirect the url to a.domain.com but use the index.html file from the a folder.

My current htaccess looks like this:

RewriteEngine on
RewriteBase /


RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

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


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%1/$1

RewriteRule ^(/)?$ %1/index.html [L]

What works For now, if I navigate to test.domain.com, it's working, it's taking the index.html file from the test folder. If I go to domain.com/test it's still working, but I want to disable this. I want to redirect domain.com/test to test.domain.com as well, but still use the index.html files from the test folder.

Another thing that is in the htacces file is to redirect www to non-www.

Any suggestions would be appreciated.

EDIT: Forgot to mention that I've tried to add this line:

RedirectMatch 301 ^/test/(.*)$ http://test.domain.com/$1

It's doing the redirection from domain.com/test to test.domain.com/index.html, but it's not loading the page right, I get an error (Page isn't redirecting properly).

Upvotes: 0

Views: 108

Answers (1)

Mangesh Sathe
Mangesh Sathe

Reputation: 2175

keep like RedirectMatch 301 ^{RELATIVE PATH}$ {ABSOLUTE_PATH}

Upvotes: 1

Related Questions